Thread: String of hex to unsigned char

  1. #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?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #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++.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  12. #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

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

Tags for this Thread