Thread: Char to ASCII Hex Value Troubles

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Char to ASCII Hex Value Troubles

    Hello!

    I need to convert ASCII characters into strings of hex values.

    I'm currently designing a program that must open a file, read its hex values, and react accordingly. I'm having trouble figuring out how to read from my file and store the 8-bit hex values in an array. Currently, I am using this code:

    Code:
    // reading a complete binary file
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    ifstream::pos_type size;
    char * memblock;
    
    int main () {
      ifstream file ("test.bin", ios::in|ios::binary|ios::ate);
      if (file.is_open())
      {
        size = file.tellg();
        memblock = new char [size];
        file.seekg (0, ios::beg);
        file.read (memblock, size);
        file.close();
    
        cout << "the complete file content is in memory\n\n";
    
        // store the hex value of the first character into a string
        string hex1 = memblock[0];
    
        delete[] memblock;
      }
      else cout << "Unable to open file";
      return 0;
    }
    Currently, the string just stores an ASCII character.

    Obviously, there is no conversion from character to hex in this code. I've tried several things, and none have worked for me. Is there some way I can directly input a file as a hex file, have the array directly store the values as hex strings, or convert the array into an array of hex strings?

    (Oh, and I don't need a "0x" or "h" in values like "0xFF" or "FFh". This will just complicated what I'm going to be doing.)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Obviously, there is no conversion from character to hex in this code.
    So this is what you need help with, right?

    A char is just an integer value that represents a character. So you want to express that integer value as the hexadecimal representation of the integer value and save it as a string. Do you know how to convert int i = 30; to "1E"? If you do, then you can use the same technique on the character.

    If you don't, then try using a stringstream or sprintf.

    BTW, don't forget to #include <string> since you are using the string class.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Yup! It works perfectly now, but there is just one tiny bug.

    I used sprintf() to write the hexadecimal equivalents to a new array. They all come out correct except one of them. The actual value is "F0", but my program outputs it as "FFFFFFF0". Do you know how I can correct this?

    Thank a lot!
    ~Matt

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should make sure you are using unsigned characters. It sounds like sprintf is converting -16 rather than 240.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Yup, that's it, but fstream won't allow reading to an unsigned character array. Is there any way I can convert a signed array into an unsigned one?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can do something like
    Code:
    sprintf(target, "&#37;02x", (unsigned int)(unsigned char)a[i]);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Hooray! It all works 100&#37; correctly. Thanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. lvalue error trying to copy between structures
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-16-2006, 06:53 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM