C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-27-2009, 03:08 AM   #1
Registered User
 
Join Date: Apr 2009
Posts: 3
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?
Harsom is offline   Reply With Quote
Old 04-27-2009, 03:10 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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
__________________
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.

Last edited by matsp; 04-27-2009 at 03:49 AM. Reason: Add missing parenthesis.
matsp is offline   Reply With Quote
Old 04-27-2009, 03:10 AM   #3
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,339
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 04-27-2009, 03:22 AM   #4
Registered User
 
Join Date: Apr 2009
Posts: 3
@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++.
Harsom is offline   Reply With Quote
Old 04-27-2009, 03:24 AM   #5
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 04-27-2009, 03:27 AM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,339
Quote:
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 04-27-2009, 03:30 AM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 04-27-2009, 03:34 AM   #8
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,339
Quote:
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.

Quote:
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 04-27-2009, 03:41 AM   #9
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by laserlight View Post
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
__________________
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 is offline   Reply With Quote
Old 04-27-2009, 03:45 AM   #10
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,339
Quote:
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
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 04-27-2009, 03:50 AM   #11
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by laserlight View Post
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
__________________
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 is offline   Reply With Quote
Old 04-27-2009, 04:11 AM   #12
Registered User
 
Join Date: Apr 2009
Posts: 3
@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
Harsom is offline   Reply With Quote
Old 04-27-2009, 04:21 AM   #13
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by Harsom View Post
@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.
matsp is offline   Reply With Quote
Reply

Tags
c++

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting bits with Unions.... Not working; please help!! dan56965 C Programming 12 08-11-2008 11:02 PM
Code review Elysia C++ Programming 71 05-13-2008 09:42 PM
Converting a circulating mouse pointer to use a Potentionmeter phoenix23 C Programming 16 10-29-2006 05:04 AM
Obtaining source & destination IP,details of ICMP Header & each of field of it ??? cromologic Networking/Device Communication 1 04-29-2006 02:49 PM
How do you search & sort an array? sketchit C Programming 30 11-03-2001 05:26 PM


All times are GMT -6. The time now is 10:39 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22