English 中文(简体)
Printing out hex values of a char* array in C gives odd values for binary input
原标题:

Here s an odd problem that s been stumping me for a bit.

The program is written in C89, and it reads a file into a char* array 16 bytes at a time (using fread and a size of sizeof(char)). The file is fopen d with the "rb" flags. The array is then passed into a function that basically takes the 16 hex values and sticks it into a string, each value seperated by a space.

Here s where the weirdness comes in. The function produces a nice hex dump, 16 bytes at a time, for a text file input that I have. But it screws up if I try it on a small bitmap image -- I end up with output in the string like ffffff88 instead of just 88.

The hex values are placed into the output string using sprintf("%02x ", input[i]); in a loop.

Why would this work properly for some files but not others?

最佳回答

What you see is the result of sign extension from the char to int, using unsigned char * or casting to unsigned char before the cast to int is (implicitly?) performed should fix your problem.

问题回答

In C the char is treated as a signed value, unless you specify it as unsigned. It seems that when you pass parameters to a function, that when the parameter happens to be a char, it s padded out to the size of a regular integer. If you don t clue the compiler in that this should be done in an unsigned way, 128 becomes 0xFFFFFF80, and so on.

So, the sign extension happens before the print formatter ever gets to look at the value. What this means is that

printf("%02X", (unsigned) input[i]);

won t solve your problem, as the value of input[i] will be sign extended, so all values from 128 to 255 are treated as -127 to -1 and become 0xFFFFFF80 to 0xFFFFFF, then cast, whereas

printf("%02X", ((unsigned char *) input)[i] );

will do the trick, but is kind of ungainly and hard to read. Best to make the type of input[] be unsigned char in the first place.





相关问题
C#: Integer value being returned as hexadecimal in VS 2008

I have a c# code snippets where i am creating a list of my custom class objects.When i am taking the count of that,its showing me a hexadecimal value in the quickwatch window. alt text http://img509....

What is the HEX code for Transparent color?

I want to set color as transparent. In RGB 255 0 255 with alpha 5 may work as transparent, But How to get it in HEX ? What is the HEX code for Transparent color

How to test a byte against a hex value?

I want to test if the byte I read from a data file is 0xEE, what should I do? I tried if (aChar == 0xEE) but doesn t seems working.

need help writing hexadecimals to an exe file

can somone tell me how to write these hexadecimals to an exe file while still having it exec 4D 5A 50 00 02 00 00 00 04 00 0F 00 FF FF 00 00 B8 00 00 00 00 00 00 00 40 00 1A 00 00 00 00 00 00 00 00 ...

how to show hex code char?

i have a file contains numbers like FB8E,FB8F,FB90 on each line. i want in my program to load this file and take each line and print the character corresponded to that number/line. for expamle, my ...

Storing hexadecimal values as binary in MySQL

I was thinking about how I m storing passwords in my database : appropriately salted SHA1 strings in a CHAR(40) field. However, since the character data in there is actually just a hex representation ...

热门标签