Friday, July 4th 2008, 11:20pm UTC+1

You are not logged in.

  • Login
  • Register

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Posts: 47

Location: Ohio, USA

Occupation: Same as my interests

1

Friday, May 16th 2008, 4:35pm

Negative values as hex?

I have the following printf statement:

printf("%02x %02d\n", 1, 1);

which outputs:

01 01

When I try to change it to a negative number, like this:

printf("%02x %02d\n", -1, -1);

I get the output:

FFFFFFFF -1

Maybe I'm just dense, but why do I get eight hexidecimal characters, when I specify in my format that I want only 2?

What would be the proper way to display a negative number in hex?

Gordon E.
===
If at first you don't succeed, cheat the second time!

This post has been edited 1 times, last edit by "grellsworth" (May 16th 2008, 4:35pm)

  • Go to the top of the page

2

Friday, May 16th 2008, 5:31pm

RE: Negative values as hex?

you need to be pretty aware of your types in terms of bit sizes. are u using 16, 32, or 64 bits?
1, in terms of hex as a signed char, is 0xFF, which, if it was unsigned happens to be 255. the MSB for signed int's, commonly known as 2's complement notation, uses 0 for positive and 1 for negative values. that leaves you with seven bits to express magnitude. so, an 8 bit value, you max out at -128 (0x80) and 127 (0x7F) ... so, once you work through 1s and 2s complement, on a 32bit int -1 convert to 0xFFFFFFFF. based on your output, you ought to be on 32 bits. this is _the_output. you can't just truncate that. 0xFFFFFFFF _represents -1 for a signed int; of course, 0xFF would correspond to the _signed_ char. since usually the compiler default is unsigned, you expect to get what you got.
  • Go to the top of the page

Posts: 47

Location: Ohio, USA

Occupation: Same as my interests

3

Friday, May 16th 2008, 5:57pm

RE: Negative values as hex?

Quoted

Originally posted by morecowbell
you need to be pretty aware of your types in terms of bit sizes. are u using 16, 32, or 64 bits?
1, in terms of hex as a signed char, is 0xFF, which, if it was unsigned happens to be 255. the MSB for signed int's, commonly known as 2's complement notation, uses 0 for positive and 1 for negative values. that leaves you with seven bits to express magnitude. so, an 8 bit value, you max out at -128 (0x80) and 127 (0x7F) ... so, once you work through 1s and 2s complement, on a 32bit int -1 convert to 0xFFFFFFFF. based on your output, you ought to be on 32 bits. this is _the_output. you can't just truncate that. 0xFFFFFFFF _represents -1 for a signed int; of course, 0xFF would correspond to the _signed_ char. since usually the compiler default is unsigned, you expect to get what you got.


I'm stepping through a char array:

Source code

1
2
3
4
5
6
7
8
9
void hexDump()
{
signed char four_chars[4] = { 1, -1, -2, -3 };

for(unsigned int x = 0; x < 4; ++x)
{
printf("%02x %02d\n", four_chars[x], four_chars[x]);
}
}


So since I'm specifying a signed char, my value should be between 0x00 and 0xFF, but I still get 0xFFFFFFFF on -1.

Do you have any other recommendations?

By the way, thank you very much for your help.

Gordon E.
===
If at first you don't succeed, cheat the second time!
  • Go to the top of the page

4

Friday, May 16th 2008, 6:38pm

if you look at the printf format tags %[flags][width][.precision][length], you'll see that width won't truncate a value if larger. to wit, try printf( " %02i", 10000). if the value is shorted than width, on the other hand, i'll pad with blanks (or zeros of so specified).
  • Go to the top of the page

Rate this thread