array to hexadecimal variable

This is a discussion on array to hexadecimal variable within the C Programming forums, part of the General Programming Boards category; Please anyone tell me how to convert array to hexadecimal variable Code: Input: a = {255, 0, 0, 0 } ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    20

    array to hexadecimal variable

    Please anyone tell me how to convert array to hexadecimal variable

    Code:
    Input:
    a = {255, 0, 0, 0 }
    
    I need output as 
    
    a = FF000000 
    
    If input is a = {255, 255, 255, 255 }
    
    Output should be 
    
    a = FFFFFFFF

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Use printf() with the %02X formatter in a loop.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    Quote Originally Posted by Dino View Post
    Use printf() with the %02X formatter in a loop.
    thanks for your reply

    OK, how to get the final value into variable

    Code:
    Say Input is 
    
    a = {255, 0, 0. 0} which is in the format a ={a, r, g, b}
    
    printf("%02x%02x%02x\n", r, g, b);
    
    how to get the final hex values into an variable ?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Use sprintf() instead of printf().
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    Quote Originally Posted by Dino View Post
    Use sprintf() instead of printf().
    Yes sprintf works if I need to store the formatted text in char. But my requirement is all about to store the hex values in int32. can anyone tell me how to do it ?

    Code:
    char color[10];
    	
    sprintf(color, "FF%02x%02x%02x", R, G, B);

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    I'm sure you don't mean that array a holds the problem as well as the solution.

    So something like this:
    Code:
    answer = a[0] << 24 | a[1] << 16 | a[2] << 8 | a[3];

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    Quote Originally Posted by nonoob View Post
    I'm sure you don't mean that array a holds the problem as well as the solution.

    So something like this:
    Code:
    answer = a[0] << 24 | a[1] << 16 | a[2] << 8 | a[3];
    so this one works fine ?
    cant i do it without sprintf ?

    Code:
    function(R, G, B)
    {
    char a[10];
    u_int32 color;
    
    sprintf(a, "FF%02x%02x%02x", R, G, B);
    color = a[0] << 24 | a[1] << 16 | a[2] << 8 | a[3];
    }

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    OK there are a few confusions here. In your latest example you are passing three integers (I assume). The function should return a composite RGB value?
    When you mentioned "Output should be a = FFFFFFFF" you meant you wanted an integer?

    Maybe
    Code:
    return a << 24 | R << 16 | G << 8 | B;
    Where 'a' is transparency or whatever? You didn't pass this value.

    If you want to print out this 32-bit integer as a hex string use
    Code:
    printf("%08X", variable);
    Last edited by nonoob; 08-13-2009 at 09:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. Joining array elements and save in variable.
    By Nutshell in forum C Programming
    Replies: 12
    Last Post: 01-12-2002, 12:06 AM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21