Thread: ANSI C ASCII String to HEX byte array conversion

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    2

    ANSI C ASCII String to HEX byte array conversion

    Apologies for such a lame question .

    Language: ANSI C

    So I was wondering if any of you have any ideas as to a good way to go about converting a string of ASCII representations of a HEX byte string e.g.:

    char buf[] = "9EF4BCDE";

    to an array of Hexidecimal bytes, equivalent to say:
    char buf[] = "\x9e\xf4\xbc\xde";

    Ideas would be appreciated.

  2. #2
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    doesn't really look like you want to convert the string. looks more like you want to preface each hex number with a 'x'.

    in any case you'll have to create a new string long enough to hold the 'x' character and that '\'.
    then you need to iterate over buf and copy its contents to the new string. placing your 'x' and '\' characters and their respective indexes.

    this may not be what you want, but that's what you asked.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Aww... come one caroundw5h - you should know by now that \x looks an awful lot like an escape sequence! As such, it would only take up on element in a char array.

    I am also a little unclear on what you are asking, phyte. Could you give us some sample out, i.e. what you are wanting to get your program to print out. Are you wanting to convert that string to a number you can perform calculations ons? Are you wanting to print binary?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char buf[] = "9EF4BCDE";
    >to an array of Hexidecimal bytes, equivalent to say:
    >char buf[] = "\x9e\xf4\xbc\xde";
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void to_hex(char buf[], int i)
    {
      if (*buf == '\0')
        return;
    
      to_hex(buf + 2, i + 1);
      buf[1] = strtol(buf, NULL, 16);
      *buf = '\0';
    }
    
    int main(void)
    {
      char buf[] = "9EF4BCDE";
      int i;
    
      to_hex(buf, 0);
    
      for (i = 1; buf[i] != '\0'; i += 2)
        printf("%#x\n", (unsigned char)buf[i]);
    
      return 0;
    }
    My best code is written with the delete key.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Aww... come one caroundw5h - you should know by now that \x looks an awful lot like an escape sequence! As such, it would only take up on element in a char array.
    you are right of course, thus my repsonse
    doesn't really look like you want to convert the string. looks more like you want to preface each hex number with a 'x'.
    I suppose my second paragraph was a little misleading. My apologies to the OP.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    2
    Thanks all.

    Thank you especially to prelude, that is exactly what I want (With a few minor tweaks).

    sean: I will be jumping into the hex buffer as the buffer contains machine code in hexidecimal format. So if i had a buffer that contained "BEFF" I needed it to contain "\xbe\xff" and so on, I apologize if my description is not clear.

    I will post the completed project tommorow for critique, it is a debugging too, bear it in mind that I do not have much C experience but am very eager to learn.

    Thank you again and I look forward to talk to you all in the future.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >that is exactly what I want
    God, I hope not. I went out of my way to avoid using a second buffer. Though a two-step process could solve the problem of holes in the array by performing the conversion and then compacting the even elements.

    >With a few minor tweaks
    Yes, I imagined that tweaks would be needed.

    >I will post the completed project tommorow for critique
    Yay! Code review! (See my new avatar and read that again )
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    What exactly is your new avatar?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What exactly is your new avatar?
    Probably the cutest anime cat I've ever seen. I don't know where from though, I found it while net diving a few weeks ago.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Probably the cutest anime cat I've ever seen.
    Uhmmm... I'm gonna have to go with your old one on that....

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >Uhmmm... I'm gonna have to go with your old one on that....

    haha I think they are both about the same.

    And as for the solution to the problem, when I read the title I thoguht you wanted what Prelude suggested, then I read your post. At that point I was a bit confused.

    Anyway I think I know what the cat is from, but I will need to double check on it. I'll get back to you on it if I'm right.

    [edit]
    Update: no idea where your avatar is from.
    [/edit]
    Last edited by master5001; 12-14-2004 at 08:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. String -> unsigned char array conversion....
    By SublicK in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2001, 12:18 PM