Thread: array to hexadecimal variable

  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
    Chappell Hill, Texas
    Posts
    2,332
    Use printf() with the %02X formatter in a loop.
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    Use sprintf() instead of printf().
    Mainframe assembler programmer by trade. C coder when I can.

  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,834
    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,834
    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, 09: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, 01: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