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:
On the third line my program outputs: "FFFFFFA9 FFFFFFA7 FFFFFFA5"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; }
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:
outside of the while loop it displays "FFFFFA9". However if I were to replace it with this code:Code:intVal = (unsigned int)'©'; sprintf(hexVal,"%X", intVal); cout << hexVal << " ";
it displays "A9" instead of "FFFFFFA9" which is what the -87 value was doing.Code:intVal = 169; // 169 is the decimal value for © sprintf(hexVal,"%X", intVal); cout << hexVal << " ";



LinkBack URL
About LinkBacks


