Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualEctara

Posted 13 December 2012 - 04:37 AM

The type const char is likely signed on your machine. Two's complement signed integers will sign-extend the value when widening to a larger type. Sign extension in two's complement counting systems will propagate the highest bit's state to all of the bits above it.

In this case, widening from signed 8bit to unsigned 32bit (depending on the compiler) copies the highest of the 8 bits into the 24 higher bits in the unsigned int.

Edit for example:

1st step: 0x3f is 00111111. The highest is 0, so zeroes are copied to the higher bits.
String is now 0x3f
2nd step: 0xe3 is 11100011. The highest is 1, so ones are copied to the higher bits.
String is now 0x3f ffffffe3
3rd step: 0x8e is 10001110. The highest is 1, so ones are copied to the higher bits.
String is now 0x3f ffffffe3 ffffff8e
4th step: 0x39 is 00111001. The highest is 0, so zeroes are copied to the higher bits.
String is now 0x3f ffffffe3 ffffff8e 39

At the end, this is the string:
0x3f ffffffe3 ffffff8e 39

Take care that you handle zeroes properly; a zero byte encountered in the number should be outputted if it isn't a leading zero.

#1Ectara

Posted 13 December 2012 - 04:29 AM

The type const char is likely signed on your machine. Two's complement signed integers will sign-extend the value when widening to a larger type. Sign extension in two's complement counting systems will propagate the highest bit's state to all of the bits above it.

In this case, widening from signed 8bit to unsigned 32bit (depending on the compiler) copies the highest of the 8 bits into the 24 higher bits in the unsigned int.

PARTNERS