# Converting UINT array of binary data to hex (C Programming)

Hi,

I've got an array of unsigned int binary characters that I would like to convert to an array of uint hex characters.

is there a standard C function for this or would I need to write one my own (and how could that look like)?

Thanks!
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
If I understand you correctly, you have:

unsigned char cbin[100]; // filled with 100 values

and you want:

unsigned int ibin[100];   // filled with equivalent 100 values

Then just have a for-loop
for(int i=0; i<100; ++i) {
ibin[i] = cbin[i];
}
0
Commented:
here...this link will definitelty help you

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

the function is itoa, which converts an integer into a string, you can pass the 3rd argument as 16 to get hex values, you'll have to go through the array and convert everything.. use strcat to append each character into a string or char array.

0
Commented:
Now that for-loop puts one char binary value into one int value. Perhaps you want to pack 4 char binary values into one int value?

One way:
unsigned int ibin[25];   // filled with equivalent 25*4 = 100 char values

for(int i=0; i<25; ++i) {
memcpy( ibin[i], cbin[4*i], 4 );
}

That packs it, but is it what you want? There are endian issues to consider.
If you have no endian issues, then you can use a union like this:

union pack{
unsigned char cbin[100]; // filled with 100 char values
unsigned int ibin[25];
};
0
Commented:
main(){
unsigned int a[5] = { 0xAAFFCCDD, 0xBBEEDDCC, 0xCCDDEEFF, 0x55443322, 0x11223344 };
for(int j=0; j<5; j++){
char b[10];
itoa(a[j],b,16);
printf("%s\n",b);
}
}

output:
aaffccdd
bbeeddcc
ccddeeff
55443322
11223344

if that's what you're looking for.
0
Commented:
If you want to convert integers to their hexadecimal representation, the result is text - if that is what you want, you could do that like
``````#include <stdio.h>

unsigned int numbers[100];
char hex[100][9]; // room for 8 chars and the terminating NULL

// fill 'numbers'

// convert:

for (int i = 0; i < 100; ++i) {

sprintf(hex[i],"%x",numbers[i]); // '%x' will format as hex
}
``````
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
worked for me - thanks!
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.