Thread: how to get hex values as output without conversion

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    3

    Question how to get hex values as output without conversion

    Hi
    I have written a code,in which i have hard coded hex values to a array (a[5]).I need these hex values which i have given to this array as ouput.On compiling the code,i am getting the converted values of these hex to int (e.g. 0x001 =1,0x002 = 2,0x0a4 = 164).

    How can i get the given hex values as output without the conversion taking place?

    Code:
    #include <stdio.h>
    
    
    int main(void) {
        int i;
         unsigned char a[5]={0x0a4,0x002,0x003,0x004};
        unsigned char p[i];
        for(i=0;i<4;i++)
        {
            p[i]=a[i];
        //    printf("%c",p[0]);
      //  memcpy(p[i],a[i],strlen(a[i]+1));
        
        printf("\n%d",p[i]);
        }
        //return 0;
    }

    pls help?!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    What do you mean? There's no actual conversion taking place, the interpretation of the data changes. If you want hexadecimal values, give "%x" or "%X" to printf().
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bharockz
    How can i get the given hex values as output without the conversion taking place?
    There is no conversion to begin with: 0x0a4 is an integer literal, just like 164. It seems that what you want to do is to print the integer in hexadecimal representation, perhaps with the "0x" prefix. If so, you need to use the appropriate format specification, e.g.,
    Code:
    printf("0x%03x\n", p[i]);
    By the way, this is wrong:
    Code:
    unsigned char p[i];
    You declared i and did not give it a specific value. I am not sure why you need p in the first place, and it is even more puzzling why you want it to be a variable length array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2015
    Location
    Philippines
    Posts
    9
    Quote Originally Posted by bharockz View Post
    Hi
    I have written a code,in which i have hard coded hex values to a array (a[5]).I need these hex values which i have given to this array as ouput.On compiling the code,i am getting the converted values of these hex to int (e.g. 0x001 =1,0x002 = 2,0x0a4 = 164).

    How can i get the given hex values as output without the conversion taking place?

    Code:
    #include <stdio.h>
    
    
    int main(void) {
        int i;
         unsigned char a[5]={0x0a4,0x002,0x003,0x004};
        unsigned char p[i];
        for(i=0;i<4;i++)
        {
            p[i]=a[i];
        //    printf("%c",p[0]);
      //  memcpy(p[i],a[i],strlen(a[i]+1));
        
        printf("\n%d",p[i]);
        }
        //return 0;
    }

    pls help?!
    Some errors to fix first
    1. In line 7, i is not initialized and should be changed to 5
    2. In line 5, though the value is fine but I think you should ommit one zero thus from 0x0a4 to 0xa4 since your array is of type character and is only one byte but 0x0a4 is 2 bytes but is still ok since the zero is irrelevant

    So the code in line 7 is not required anymore.
    The above solution is correct you can use "0x%02x" or "0x%03x" instead or "%d"

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    3
    Thank you guys!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-13-2012, 07:50 PM
  2. Unable to print the output of a conversion problem
    By abhishekcoder in forum C Programming
    Replies: 1
    Last Post: 04-13-2012, 11:16 AM
  3. Not able to print the output of the conversion problem
    By abhishekcoder in forum C Programming
    Replies: 11
    Last Post: 04-13-2012, 09:41 AM
  4. Passing values to arrays? Weird output?
    By Loic in forum C++ Programming
    Replies: 13
    Last Post: 05-10-2007, 08:43 AM
  5. Zero Values being ignored for output (hexadecimal)
    By Oldman47 in forum C++ Programming
    Replies: 5
    Last Post: 12-12-2006, 04:01 PM

Tags for this Thread