Thread: Convert hex byte array to hex char*

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    12

    Convert hex byte array to hex char*

    I have byte array like aa,bb,12,77

    I want to convert it to char* so when I do

    Printf("%s",newCharPtr);

    I will get aabb1277

    How can I do it please?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you reeeeeeeeeeeeeeeeeeeally want a string, or do you just want to print it in hex?

    Either way I would suggest using the %x specifier for printf in a loop (either printf to the screen or sprintf to make a string); if you're planning to make a string out of it, you will need more space allocated, because the byte aa and the string "aa" aren't compatible with each other.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    12
    Quote Originally Posted by tabstop View Post
    Do you reeeeeeeeeeeeeeeeeeeally want a string, or do you just want to print it in hex?

    Either way I would suggest using the %x specifier for printf in a loop (either printf to the screen or sprintf to make a string); if you're planning to make a string out of it, you will need more space allocated, because the byte aa and the string "aa" aren't compatible with each other.
    Yes ,I want to convert this byte array to char* that I can see
    aabb1277 when I use %s, not %x.

    I dont have a problem to allocate bigger memory

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by C progammer View Post
    Yes ,I want to convert this byte array to char* that I can see
    aabb1277 when I use %s, not %x.

    I dont have a problem to allocate bigger memory
    Sounds like you're going to be using sprintf[fn 1] in a loop, then. (Note that while "bigger" is important, so is "different" -- you won't be able to use the same memory for both.) You will probably want to use %0x so that leading zeroes get printed into your string; you'll have to move forward two spaces in your string buffer at a time, since you'll be printing two characters at a time; you'll need to make sure there's a \0 at the end. You can malloc exactly the right amount of space at the beginning, since you know you'll need 2n+1 bytes to store the string.

    [fn 1]If for some reason you're going to be doing this a zillion times and you don't want to call sprintf all that time, or you just think it would impress people, you can try to use a hash-table of characters and use the byte (or some bit-masked version of the byte) as an index to grab each character/pair of characters and copy them into your buffer. I don't know that I would start there, though.

    EDIT: Sitting down with a piece of paper, the hash-table method isn't as complicated as I had originally thought, so maybe you could start there after all.
    Last edited by tabstop; 11-01-2018 at 03:58 PM.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,136
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main() {
        unsigned char a[] = {0xab,0xcd,0xef,0x0f,0x0e,0x0d,0x0c};
     
        char *s = malloc(sizeof a * 2 + 1);
        for (size_t i = 0; i < sizeof a; i++)
            sprintf(s + i * 2, "%02x", a[i]);
     
        printf("%s\n",s);
        free(s);
     
        return 0;
    }
    Last edited by john.c; 11-01-2018 at 04:45 PM.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  6. #6
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Hi!
    I'am new here. My Nickname should be rustyoldguy. Sorry my failure. Now to you.
    I think you can use also this here:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
     char hexvalue[100];
    
     union xval {
     unsigned char ubytearray[4];
     unsigned int icompval;
     } xhex;
    
     xhex.ubytearray[0] = 0x77;
     xhex.ubytearray[1] = 0x12;
     xhex.ubytearray[2] = 0xbb;
     xhex.ubytearray[3] = 0xaa;
    
     printf("Value as decimal: %u as hex:%x\n", xhex.icompval, xhex.icompval);
     sprintf(hexvalue, "%x", xhex.icompval);
    
     printf("as string: %s\n", hexvalue);
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,136
    Due to endianness, you can't be sure what order those bytes will print on a particular system (so it's not portable). Also, you presumably want to say "%08x".
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-09-2013, 07:06 PM
  2. Convert string to char or use 2d array of char?
    By simpleblue in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2011, 05:00 PM
  3. Convert char array to int
    By McFry in forum C Programming
    Replies: 9
    Last Post: 07-12-2007, 12:40 AM
  4. How to Convert a byte datatype to char or string
    By kvp_vivek in forum C Programming
    Replies: 3
    Last Post: 06-17-2006, 02:20 PM
  5. Getting a 2 byte number from an array of char's
    By kzar in forum C Programming
    Replies: 20
    Last Post: 04-10-2005, 10:18 PM

Tags for this Thread