Hi Guys
Hope you can help me out.
I have a string of hex, say:
I want to take this string at convert it to an array of char:Code:S = "123456ABCDEF"
Can you help?Code:unsigned char CharHex[] = { 0x1, 0x2, 0x3... 0xA..0xF}
Hi Guys
Hope you can help me out.
I have a string of hex, say:
I want to take this string at convert it to an array of char:Code:S = "123456ABCDEF"
Can you help?Code:unsigned char CharHex[] = { 0x1, 0x2, 0x3... 0xA..0xF}
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:
Edit2: Code that avoids UB and has been tested.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); }
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
Last edited by matsp; 04-27-2009 at 03:49 AM. Reason: Add missing parenthesis.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
@matsp: I agree with you. I'ld actually prefer your way:
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 roadCode:unsigned char CharHex[] = { 0x12, 0x34, 0x56... 0xAB..0xEF}
@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++.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Looks more like a compile error than a bug to me, but I'm resisting testing with my compiler.Originally Posted by matsp
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Actually, that wasn't what I was referring to, but that's a problem too - fixed now.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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 Harsom
I think turning your macro into an inline function would still be obfuscated enough but without undefined behaviour.Originally Posted by matsp
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Well, until you have your compile errors and undefined behaviour fixed, it does not matter if the value should be 0x41 or 0x37Originally Posted by matsp
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
@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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.