# Thread: String of hex to unsigned char

1. ## String of hex to unsigned char

Hi Guys
Hope you can help me out.

I have a string of hex, say:
Code:
` S = "123456ABCDEF"`
I want to take this string at convert it to an array of char:
Code:
`unsigned char CharHex[] = { 0x1, 0x2, 0x3... 0xA..0xF}`
Can you help?

2. Well, technically, I'd expect that to convert to 0x12, 0x34... 0xEF.

Beyond that, you'll have to show us some effort before we show you ours.

Edit: Obfuscated:
Code:
```typedef unsigned char u8;
#define a(b) ((*b>071)?*b++-0x41:*b++-060)
void s2u(char *s, u8 *o)
{
for(;*s;o++) *o = (a(s) << 4)|a(s);
}```
Edit2: Code that avoids UB and has been tested.
Code:
```typedef unsigned char u8;
inline u8 a(char **b) { u8 r; if(**b>071) r= **b-0x37; else r = **b-060; (*b)++; return r; }
void s2u(char *s, u8 *o)
{
for(;*s;o++) *o = (a(&s) << 4)|a(&s);
}```

--
Mats

3. If you just need it to be an array of char rather than an array of unsigned char, then the data() member function would be handy. Otherwise, assuming that the string length is variable, one option would be to create a std::vector<unsigned char> with the string's contents instead.

EDIT:
Grr... next time mind saying exactly how you want the conversion to be done? Admittedly, I did not read your code snippet carefully.

4. @matsp: I agree with you. I'ld actually prefer your way:
Code:
`unsigned char CharHex[] = { 0x12, 0x34, 0x56... 0xAB..0xEF}`
I'ld like to show you some stuff, but asked for hints as I am quite new to this and I dont want to take the wrong road

@laserlight: Thanks for the hint, but unsigned char is a must since I have to have the result in this way.
Regarding the conversion: I'm open for all hints, I just want to keep it simple in an object oriented manner/C++.

5. What, my hint was no good? [Actually, I just noticed that there is a bug in my code - can anyone spot it?]

I still think you should post some code. Or explain what you are stuck at in some other way.

--
Mats

6. Originally Posted by matsp
Actually, I just noticed that there is a bug in my code - can anyone spot it?
Looks more like a compile error than a bug to me, but I'm resisting testing with my compiler.

7. Actually, that wasn't what I was referring to, but that's a problem too - fixed now.

--
Mats

8. Originally Posted by Harsom
Thanks for the hint, but unsigned char is a must since I have to have the result in this way.
I concur with matsp: it would be good if you show us a more concrete code example. After all, we do not even know the type of S, so I made an assumption that could have been unwarranted.

Originally Posted by matsp
Actually, that wasn't what I was referring to, but that's a problem too - fixed now.
I think turning your macro into an inline function would still be obfuscated enough but without undefined behaviour.

9. Originally Posted by laserlight
I think turning your macro into an inline function would still be obfuscated enough but without undefined behaviour.
Yeah, ok, fair point about UB. That's still not what I was referring to. It's the value 0x41 that should be 0x37!

--
Mats

10. Originally Posted by matsp
That's still not what I was referring to. It's the value 0x41 that should be 0x37!
Well, until you have your compile errors and undefined behaviour fixed, it does not matter if the value should be 0x41 or 0x37

11. Originally Posted by laserlight
Well, until you have your compile errors and undefined behaviour fixed, it does not matter if the value should be 0x41 or 0x37
I have updated the original code with a "without ub and correct constants".

To be honest, it was more of a joke anyways...

@Harsom: Are you aware that your string is an uneven number of characters, which means that you can not create a full number of unsigned chars - there will be an 'F' on the end of the string that hasn't got a second character to make a complete hex number from.

--
Mats

12. @Matsp: My plan is to add 0 to the last character if there is an uneven number. But for now I'll focus on even number scenarios That will be enough for me in the first round.
I'll get back with a code later today. To be honost your code confuses me more but thanks anyway for trying

13. Originally Posted by Harsom
@Matsp: My plan is to add 0 to the last character if there is an uneven number. But for now I'll focus on even number scenarios That will be enough for me in the first round.
I'll get back with a code later today. To be honost your code confuses me more but thanks anyway for trying
Obfuscation - Wikipedia, the free encyclopedia
Particularly: "more difficult to interpret." is entirely intended.

--
Mats