1

I have the following C string

"72e4247d3c91f424c62d909d7c1553a5"

It consists of 32 hex digits. It is an array with 4 integers in hex. How can I get back the numbers in form of integers from this array?

Clifford
  • 88,407
  • 13
  • 85
  • 165
harry77
  • 107
  • 2
  • 9
  • 1
    32 "digits", comprising only one number. As to how you can get them back -- do you have the code that did the initial conversion? Just reverse it. Most likely, take 8 digits at a time and convert each to its integer representation, perhaps using sscanf(). – mah Jun 03 '13 at 15:35
  • There are easily a dozen ways to convert character hex into binary. (But do you really have character hex, or is that just a hex dump of the binary value of the string?) – Hot Licks Jun 03 '13 at 15:37
  • @Hot Licks, it is really number. i turned it with sprintf and now i want to take back the initial value. but unfortunately i cannot do it. – harry77 Jun 03 '13 at 16:09
  • That was clear as mud. First off you need to understand the difference between a character string which contains characters 0-F representing a hex value, vs a character string containing almost anything but "dumped" in hex. Do you know the difference? – Hot Licks Jun 03 '13 at 16:12
  • i think so. correct me if i am wrong. i have arrays with 4 numbers, which when i try to print them with printf("%02x", ((unsigned char*)mdContext)[i]) i=[0,16], i get my wanted number with 32 digits. so is this on hex or not?? – harry77 Jun 03 '13 at 16:20
  • I'm puzzled. If you created the string that way, why not just reference mdContext and fetch your data from it? – Hot Licks Jun 03 '13 at 19:08

2 Answers2

4

You'll have to parse the four 32-bit/8 hex digit chunks separately. The easiest way to do that is

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

void parse_hex4(char const *str, uint32_t num[4])
{
    char buf[9];  // 8 hex digits + NUL

    buf[8] = '\0';
    for (int i=0; i<4; i++) {
        memcpy(buf, str + i * 8, 8);
        num[i] = strtoul(buf, NULL, 16);
    }
}

This assumes str is zero-padded to be exactly 32 characters long and it does no input validation. If you're using the compiler from Redmond that is stuck in the 1980, use unsigned long instead of uint32_t.

Community
  • 1
  • 1
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Doesn't your function need to add the terminator to `buf` and add a `str+=8`? – simonc Jun 03 '13 at 15:40
  • @simonc: thanks, added that, and a caveat that the string should be properly padded. – Fred Foo Jun 03 '13 at 15:42
  • It seems to me you could run afoul of endianness. – Hot Licks Jun 03 '13 at 19:06
  • @HotLicks: The endianness of `strtoul`'s input is fixed and so is the endianness of `num`: both are little-endian. It's only inside the individual `uint32_t`s that endianness varies, but that's always true in C and it doesn't matter for the problem at hand. – Fred Foo Jun 04 '13 at 09:28
  • Yes, but the endianness of the OP's discombobulation of the data is not fixed. – Hot Licks Jun 04 '13 at 11:04
0

Have you tried strtol ?

int val = (int) strtol (hex_string, NULL, 16);

Repeat on substrings if it is a flattened array.

Regards

wizard
  • 1,456
  • 1
  • 11
  • 20