1. ## 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. Use printf() with the %02X formatter in a loop.

3. Originally Posted by Dino
Use printf() with the %02X formatter in a loop.

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. Use sprintf() instead of printf().

5. Originally Posted by Dino
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. 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. Originally Posted by nonoob
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. 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);

Popular pages Recent additions