Re: char to hex



Juunis wrote:
Hi!

I have a small problem.
I have a char string, which contains hex codes as ascii.

char a[] = "0xF0DE0xEEC10x0034";

I want to convert the string so, that each hex code will be converted
exactly like those are in string: So the result should be like below:

char16bit[0] = 0xF0DE;
char16bit[1] = 0xEEC1;
char16bit[2] = 0x0034;

Any ideas? Despite this should be a small problem, it has stoped me a
while....:/

Thanks!
- Juunis

You don't explain you problem very well so this may not be the solution.


#include <stdio.h> #include <string.h> #include <stdlib.h>

#define NUM 3

char a[] = "0xF0DE0xEEC10x0034";

typedef unsigned short ushrt;

ushrt char16bit[NUM];

int main(void) {
   int i;
   char tmp[7] = {0};
   for (i = 0; i < NUM; ++i) {
      strncpy(tmp, a + i * 6, 6);
      char16bit[i] = (ushrt)strtol(tmp, NULL, 0);
   }
   printf("char a[] looks like %s\n", a);
   for (i = 0; i < NUM; ++i)
      printf("char16bit[%d] = 0x%04X\n", i, char16bit[i]);
   return 0;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
                    --- Albert Einstein ---
.