![]() |
| | #1 |
| Registered User Join Date: Apr 2009
Posts: 3
| String of hex to unsigned char Hope you can help me out. I have a string of hex, say: Code: S = "123456ABCDEF" Code: unsigned char CharHex[] = { 0x1, 0x2, 0x3... 0xA..0xF}
|
| Harsom is offline | |
| | #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);
}
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 | |
| | #3 |
| C++ Witch 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 | |
| | #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}
![]() @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 | |
| | #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 | |
| | #6 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,339
| Quote:
__________________ 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 | |
| | #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 | |
| | #8 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,339
| Quote:
Quote:
__________________ 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 | |
| | #9 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- 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 | |
| | #10 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,339
| Quote:
__________________ 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 | |
| | #11 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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 | |
| | #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 | |
| | #13 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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 | |
![]() |
| Tags |
| c++ |
| Thread Tools | |
| Display Modes | |
|
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 |