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?

6
  • 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(). Commented Jun 3, 2013 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?) Commented Jun 3, 2013 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. Commented Jun 3, 2013 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? Commented Jun 3, 2013 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?? Commented Jun 3, 2013 at 16:20

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't your function need to add the terminator to buf and add a str+=8?
@simonc: thanks, added that, and a caveat that the string should be properly padded.
It seems to me you could run afoul of endianness.
@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_ts that endianness varies, but that's always true in C and it doesn't matter for the problem at hand.
Yes, but the endianness of the OP's discombobulation of the data is not fixed.
0

Have you tried strtol ?

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

Repeat on substrings if it is a flattened array.

Regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.