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

rempas rempas at tutanota.com
Thu Dec 30 09:57:39 UTC 2021

On Sunday, 26 December 2021 at 17:35:12 UTC, Siarhei Siamashka 
> I don't think that this makes any sense. Numbers can be 
> negative regardless of the base that is used to show them. If 
> "decimal was supposed to be the only base that will make sense 
> for humans to read", then why bother implementing "this 
> function that converts a number to a string and it can return 
> it in any base you want"?

When I say that it is the only base that makes sense to read, I 
mean that it is the only base that is in the base we learn to 
read and write and it used for a general purpose and not for 
specific tasks (like to shorten memory addresses for example). 
Binary is also very common because this is what the machine 
understands and it is also very easy to go from hex to binary and 
the opposite. This is also why they bothered adding an official 
way of showing negative binary and doing mathematic operations 
with it (the most significant bit identifies if the number is 
negative or positive).

> BTW, this is how Ruby and Crystal languages handle conversion 
> between strings and integers:
> ```Ruby
> puts -10.to_s(16)     # prints -a
> puts -10.to_s(2)      # prints -1010
> puts 0x100.to_s(10)   # prints 256
> puts "10".to_i(2)     # prints 2
> puts "-100".to_i(8)   # prints -64
> ```
> Basically, strings have method ".to_i" and integers have method 
> ".to_s". A single optional argument specifies base (between 2 
> and 36) and defaults to 10.

This is exactly how my library handles them too!

More information about the Digitalmars-d mailing list