Results 1 to 9 of 9

Thread: c++ convert from char[] to hex

  1. #1
    Join Date
    Sep 2007
    Location
    Derby, UK
    Beans
    510
    Distro
    Ubuntu 13.04 Raring Ringtail

    c++ convert from char[] to hex

    I want to convert an array of chars comprising of numbers, no letters into an array of hexadecimal numbers which i assume will have to be of type string.

    My arrays are:
    char buffer[10]
    string bufHex[10]


    so if buffer[0] = '101', then bufHex[0] = "65"

    How do i do this?

  2. #2
    Join Date
    Mar 2009
    Beans
    132

    Re: c++ convert from char[] to hex

    char temp[16];
    tempValue[0] = 0;
    // put below in a loop for each character
    sprintf(temp,"0x%04X, ", Buffer[i]);
    strcat(tempValue, temp);

    not sure if that will work

  3. #3
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: c++ convert from char[] to hex

    Code:
    stringstream ss;
    ss << hex << (int)buffer[0];
    bufHex[0] = ss.str();

  4. #4
    Join Date
    Oct 2008
    Location
    /home/brad
    Beans
    591
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: c++ convert from char[] to hex

    I'm not sure if you are working with numbers that you need to go about converting - the array indexes/values can be accessed the same way - just add "0x" to the beginning - saying "somearray[0xA]" is the same as "somearray[10]".
    I could be wrong/misunderstanding the question, but I'm pretty sure that's how it works.
    Windows free as of 11/18/2008.
    Feel free to message me on Skype if you have questions on anything. I'm happy to help!
    Note: If things don't work, try this: Reboot. Check all wires. Check Again.

  5. #5
    Join Date
    Sep 2007
    Location
    Derby, UK
    Beans
    510
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: c++ convert from char[] to hex

    Got it working.

    Code:
    char buffer[10];
    char bufHex[10];
    
    for(j = 0; j < 10; j++)
        sprintf(bufHex, "%X", buffer[j]);

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: c++ convert from char[] to hex

    Quote Originally Posted by Woody1987 View Post
    Got it working.

    Code:
    char buffer[10];
    char bufHex[10];
    
    for(j = 0; j < 10; j++)
        sprintf(bufHex, "%X", buffer[j]);
    Nice C code!

  7. #7
    Join Date
    Sep 2007
    Location
    Derby, UK
    Beans
    510
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: c++ convert from char[] to hex

    Quote Originally Posted by dwhitney67 View Post
    Nice C code!
    If it works, it works.

  8. #8
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: c++ convert from char[] to hex

    Quote Originally Posted by Woody1987 View Post
    If it works, it works.
    But does it? You are overwriting bufHex over and over. In the end it'll just contain the hex string of buffer[9] ...

  9. #9
    Join Date
    Sep 2007
    Location
    Derby, UK
    Beans
    510
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: c++ convert from char[] to hex

    Quote Originally Posted by geirha View Post
    But does it? You are overwriting bufHex over and over. In the end it'll just contain the hex string of buffer[9] ...
    every iteration i do something with bufhex, so it doesnt matter if it gets overwritten.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •