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!
php-newbieAsked:
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.

phoffricCommented:
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
vidularandunuCommented:
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
phoffricCommented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

vidularandunuCommented:
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
jkrCommented:
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
}

Open in new window

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.

Start your 7-day free trial
php-newbieAuthor 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.