Reading a file in hexidecimal

This is a discussion on Reading a file in hexidecimal within the C++ Programming forums, part of the General Programming Boards category; Im haveing trouble reading a file in hexidecimal. Lets say I have a file called "something.txt" which goes as follows: ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    112

    Reading a file in hexidecimal

    Im haveing trouble reading a file in hexidecimal. Lets say I have a file called "something.txt" which goes as follows:

    Hello
    1234567890

    !@#$%^&*()

    My program display lines 1,2, and 4 correctly but I get some strange errors on line 3. This is my code:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    
    int main()
    {
    	char charVal[1] = "";
    	char hexVal[3]  = "";
    	char charSingle = ' ';
    	int  intVal     =  0;
    	ifstream file("something.txt", ios::binary);
    	while(!file.eof())
    	{
    		file.read(charVal, 1);
    		charSingle = charVal[0];
    		intVal = (unsigned int)charSingle;
    		sprintf(hexVal,"%X", intVal);
    		cout << hexVal << " ";
    	}
    	return 0;
    }
    On the third line my program outputs: "FFFFFFA9 FFFFFFA7 FFFFFFA5"

    If I replace the last line in my code to "cout << intVal << " ";" Then it outputs: "-87 -89 - 91". Why am I getting negatives? If I put this code:
    Code:
    intVal = (unsigned int)'';
    sprintf(hexVal,"%X", intVal);
    cout << hexVal << " ";
    outside of the while loop it displays "FFFFFA9". However if I were to replace it with this code:
    Code:
    intVal = 169; // 169 is the decimal value for 
    sprintf(hexVal,"%X", intVal);
    cout << hexVal << " ";
    it displays "A9" instead of "FFFFFFA9" which is what the -87 value was doing.

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Typecasting problem

    I looks like when you convert your signed char to an unsigned int, It's not "throwing-away" the sign bit. Which makes sense.

    This seems to work:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    
    int main()
    {
    	unsigned char charVal[1] = "";
    	char hexVal[3]  = "";
    	unsigned char charSingle = ' ';
    //	int  intVal     =  0;
    	ifstream file("c:\\C++ Programs\\something.txt", ios::binary);
    	while(!file.eof())
    	{
    		file.read(charVal, 1);
    		charSingle = charVal[0];
    //		intVal = (unsigned int)charSingle;
    		sprintf(hexVal, "%X", charSingle);
    		cout << hexVal << " ";
    	}
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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