0

today I tried to convert a hex string to an unsigned char[]

string test = "fe5f0c"; unsigned char* uchar= (unsigned char *)test.c_str(); cout << uchar << endl; 

This resulted in the output of

fe5f0c 

hrmpf :-(. The desired behaviour would be as follows:

unsigned char caTest[2]; caTest[0] = (unsigned char)0xfe; caTest[1] = (unsigned char)0x5f; caTest[2] = (unsigned char)0x0c; cout << caTest << endl; 

which prints unreadable ascii code. As so often I am doing something wrong ^^. Would appreciate any suggestions.

Thanks in advance

5
  • 2
    Your array is too small, you cannot assign to element 2 of an array of size 2. Commented May 15, 2011 at 0:27
  • Also, what output do you expect when printing a char array? You will get the characters with the ASCII (or whatever system your computer uses) codes 254, 95, 12 and so on. What is the output you actually want? That is not clear from your question. Commented May 15, 2011 at 0:31
  • 1
    I think you meant unsigned char caTest[4]; and forgot caTest[3] = 0;, but still it's not clear what you want. It looks like you want to send the ASCII representations of hex-pair values to stdout, regardless of whether they're printable. Commented May 15, 2011 at 0:34
  • Hmm, I originally meant caTest[3]; but if you could point out why I need caTest[3]=0 I would also be happy with caTest[4]. As for my intention, I tried to do what pablo described in his answer. Thanks for helping me out. Commented May 15, 2011 at 9:33
  • @ftiaronsem: You're treating caTest as a string. Strings are null-terminated. If you don't do this, then you'll end up reading past the end of the buffer. Commented May 15, 2011 at 11:51

3 Answers 3

2

Sure, you just have to isolate the bits you are interested in after parsing:

#include <string> #include <cstdlib> #include <iostream> typedef unsigned char byte; int main() { std::string test = "40414243"; unsigned long x = strtoul(test.c_str(), 0, 16); byte a[] = {byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x), 0}; std::cout << a << std::endl; } 

Note that I changed the input string to an eight digit number, since otherwise the array would start with the value 0, and operator<< would interpret that as the end and you wouldn't be able to see anything.

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

4 Comments

Thank you very much for your answer, unfortunatelly thats not what I wanted to do, my plan was to do what Pablo described in his post. Do you have some suggestions of how I could implement that. I would greatly appreciate your help.
@fti: I updated my post. Does that give the intended results?
wow, thats great, perfect. Since the string is rather long, i will have to split it and run a for loop over the code you gave me. But i can handle this (at least I hope so ;-)). Thanks for your help.
@fti: In that case, it is probably easier to process only one byte at a time inside the for loop.
1

"fe5f0c" is a string of 6 bytes (7 containing the null terminator). If you looked at it as an array you would see:

char str[] = { 102, 101, 53, 102, 48, 99 }; 

But you want

unsigned char str[] = { 0xfe, 0x5f, 0x0c }; 

The former is a "human readable" representation whereas the latter is "machine readable" numbers. If you want to convert between them, you need to do so explicitly using code similar to what @Fred wrote.

Casting (most of the time) does not imply a conversion, you just tell the compiler to trust you and that it can forget what it thinks it knows about the expression you're casting.

1 Comment

thanks, this is exactly what i want. Do you have some code hints how that conversion should look like? That would really be great.
-1

Here is a simpler way for hexadecimal string literals:

unsigned char *uchar = "\xfe\x5f\x0c"; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.