It doesn’t “convert” really, it’s just that
0x0001 is how the
UInt16 representing the integer value 1 is printed. Each hexadecimal digit is four bits (2^4 = 16), so eight bits (aka a byte) is two hex digits. So a
UInt8 which is eight bits is exactly two hexadecimal digits. Similarly, a
UInt16 is 16 bits, which is exactly four hex digits. Julia prints unsigned values with the full number of hex digits they can represent, even if the leading digits are zeros. This has a few benefits:
- If you cut and paste the printed form an unsigned number back into the REPL (interactive prompt), you get the same value and type back;
- If you print a bunch of these values they always have the same width so they tend to align nicely in the printed output.
We treat unsigned and signed integers differently based on this observation: when people are working with unsigned integers, they are often interested in specific bit patterns, which are easier to understand in hexadecimal. When people care more about integer arithmetic, they tend to use signed types, which can also represent negative values, and they want to see the values in decimal so that’s how we print signed integers. When numbers are printed in decimal, the nice correspondence between digits and bits doesn’t work because 10 isn’t a power of 2, so we just print signed integers normally without any leading zeros. This means that a signed integer value can lose its type when you copy it and paste it back into a program, but that’s usually not a big issue since most people have 64-bit machines these days and 2^63 is plenty big for most things.
I do not know whether I have to put it here or I have to create a new topic?
Since it’s closely related, it’s better to ask in this thread. If it was a totally different topic it would be better to start a new thread.
I apologize again for asking this.
Don’t worry at all. That’s what this forum is for!