How to change HEX String to Byte Array

This is a discussion on How to change HEX String to Byte Array within the C Programming forums, part of the General Programming Boards category; Hi All, I want to change HEX String to Byte Array, but I can't get the expected result. Code: //Hex ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    49

    How to change HEX String to Byte Array

    Hi All, I want to change HEX String to Byte Array, but I can't get the expected result.

    Code:
    //Hex String
    char* HEXStr="A4505D0B0F6AEDAA";
    
    unsigned char* tmpByte=malloc(9);
    for(int i=0; i<16; i++)
    {
      sscanf(HEXStr, "02X", &tmpByte[i]);
      HEXStr+2*sizeof(char);
    }
    
    //Change it to Hex
    char* backHex=malloc(17);
    for(int i =0; i<8; i++)
    {
      sprintf(backHex+2*i, "02X", tmpByte[i]); 
    }
    HEXStr ="A4505D0B0F6AEDAA";
    Result of backHEX="FF505D0B0F6AFFFF";

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    Someone was having trouble before with
    Code:
    //Hex String
    char* HEXStr="A4505D0B0F6AEDAA";
    The changed it to
    Code:
    //Hex String
    char HEXStr[]="A4505D0B0F6AEDAA";
    It gave the string a place in memory

    I think that you are not showing all your code (right?) - I'm saying that because you are not freeing any memory you are "mallocing".

    Code:
    unsigned char* tmpByte=malloc(9);
    for(int i=0; i<16; i++)
    {
      sscanf(HEXStr, "02X", &tmpByte[i]); // <--------- For i up to 16?!

    I think that you should get rid of the mallocs

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't understand how you got any result at all. Your format specifiers are completely wrong (hint: they start with a percent sign). The line: HEXStr+2*sizeof(char); does nothing at all.
    Code:
    #include <stdio.h>
    
    int main() {
        int i, n;
        char *HEXStr = "A4505D0B0F6AEDAA";
        unsigned char tmpByte[9];
        char backHex[17];
    
        for(i = 0; i < 8; i++) {
            sscanf(HEXStr+2*i, "%2X", &n);
            tmpByte[i] = (char)n;
        }
    
        for(i = 0; i < 8; i++)
            sprintf(backHex+2*i, "%02X", tmpByte[i]);
        backHex[16] = '\0';
    
        printf("%s\n", backHex);
        return 0;
    }
    Last edited by oogabooga; 08-13-2012 at 12:17 AM. Reason: fixed array overrun (changed 16 to 8 in first loop)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    This is not the exact code that you compiled, is it? Can you copy and paste the actual code that gives the results you posted?

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    I think for the most part you are just doing unnecessary calculations and using unnecessary temporary space.

    One of the reasons hexadecimal is so nice is because 1 hexa digit is equal to a nibble or 4 bits, so if I were supposed to convert your string into a byte string it would have to be like:
    Code:
    A4	 50 	 5D 	  0B 	   0F 	    6A	     ED	      AA
    10100100 0101000 01011101 00001011 00001111 01101010 11101101 10101010
    Each column is one byte of string. BTW this is for illustration. Just don't mind the numbers too much, I tried to convert to binary and I think it's right, but I might have transposed some digits or something.

    The good news is that sscanf can indeed do this work, but you need to handle it two characters at a time, because it is the same as storing the result one byte at a time.

    Additionally, if you actually look up the %X conversion for sscanf (check your syntax!) you will find that the result is expected to be a unsigned int. Just use a temporary variable with sscanf, and cast over to unsigned char or char, whichever is right, when you put it into the byte string. I am fairly sure this was completely ignored in your attempt and contributed to things being very wrong.
    Last edited by whiteflags; 08-13-2012 at 01:07 AM.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    Code:
    #include <stdio.h>
     
    int main() {
        int i, n;
        char *HEXStr = "A4505D0B0F6AEDAA";
        unsigned char tmpByte[9];
        char backHex[17];
     
        for(i = 0; i < 16; i++) {
            sscanf(HEXStr+2*i, "%2X", &n);
            tmpByte[i] = (char)n;
        }
     
        for(i = 0; i < 8; i++)
            sprintf(backHex+2*i, "%02X", tmpByte[i]);
        backHex[16] = '\0';
     
        printf("%s\n", backHex);
        return 0;
    }
    Whoops

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Endian change & Byte Array -> Struct
    By bramaen in forum C Programming
    Replies: 3
    Last Post: 02-05-2010, 12:55 PM
  2. byte order change
    By onebrother in forum C Programming
    Replies: 1
    Last Post: 08-06-2007, 06:40 AM
  3. Hex String to byte array conversion
    By IfYouSaySo in forum C# Programming
    Replies: 3
    Last Post: 06-15-2006, 05:59 PM
  4. ANSI C ASCII String to HEX byte array conversion
    By phyte in forum C Programming
    Replies: 10
    Last Post: 12-14-2004, 08:02 AM
  5. Adding a byte to a string
    By psul in forum C++ Programming
    Replies: 6
    Last Post: 04-22-2004, 03:11 PM

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