Right way to show numbers in binary/hex/octal in your opinion?

Rumbu rumbu at rumbu.ro
Mon Dec 27 06:55:37 UTC 2021

On Saturday, 25 December 2021 at 21:12:33 UTC, rempas wrote:
> So I have this function that converts a number to a string and 
> it can return it in any base you want. However, for negative 
> numbers, the result ...

When people are dumping numbers to strings în any other base than 
10, they are expecting to see the internal representation of that 
number. Since the sign doesn't have a reserved bit in the 
representation of integrals (like it has for floats), for me it 
doesn't make any sense if I see a negative sign before a hex, 
octal or binary value.

The trickiest value for integrals is the one with the most 
significant bit set (e.g. 0x80). This can be -128 for byte, but 
also 128 for any other type than byte. Now, if we go the other 
way around and put a minus before 0x80, how do we convert it back 
to byte? If we assume that 0x80 is always 128, -0x80 will be -128 
and can fit a byte. On the other side, you cannot store +0x80 in 
a byte because is out of range.

This is also an issue în phobos:


More information about the Digitalmars-d mailing list