I found out that everything in range of 0x7F to 0xFF is displayed as 0xFF. Strange... |
AHA. I see now. I thought this might be a problem, but I didn't think it would cause the symptoms you were describing.
The problem: 'buff' is signed. Change it to 'unsigned char'.
Also, make 'hb' at least 1 char bigger (or, preferably, get rid of these functions and use stringstream instead)
-------------
Why this (and sprintf) is bad:
You are corrupting the stack. It's quite possible this code may crash your program at a future date, or cause other very weird, hard to find errors.
Effectively what you are doing is this:
1 2 3
|
char v = 0x90;
char buf[2];
sprintf(foo,"%02X",v);
|
Problem 1) Even if this prints as expected, 'buf' is not big enough because sprintf will print
3 characters (two digits, followed by the null terminator). IE: Buffer overflow, stack corruption.
Problem 2) '0x90' when stored in a [i]signed[i] char is -112. Not +144 like you may expect.
Problem 3) sprintf is not type safe. It has no way to know 'v' is a char. Given that you are doing "%X", it is probably assuming 'v' is an unsigned int. And -112 when cast to an unsigned int is: 0xFFFFFF90. Therefore sprintf is actually printing 9 characters: "FFFFFF90" followed by the null terminator.
IE: even more buffer overflow and stack corruption. Also, since you're only printing the first 2 characters of this sequence, that's why you keep getting FF.