5

Hexadecimal goes from 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.

I am confused as to where the 'x' in '0x0f' comes from.

And if '0x0f' = '0000 1111' or '15' why not just write '0f'.

Pretty sure this is universal across most languages, but if not, I'm using Java.

Thank you.

keitereth24
  • 117
  • 7
seamus
  • 2,681
  • 7
  • 26
  • 49
  • It's not actually universal (though it's common). For example, in some assembly languages you'll see `$1234`. – harold Feb 13 '14 at 08:58
  • 1
    This comes from `C` which does the same. This is called "hexadecimal" and you could use `h` but this is very close to `g`, `e` cannot be used as it is used in hex already `x` is obviously different. – Peter Lawrey Feb 13 '14 at 08:59
  • 1
    Hexadecimal actually only has 16 digits (0-9, A-F), not 17 like you listed. `G` is not a hexadecimal digit. – 8bittree Feb 17 '17 at 20:37

2 Answers2

14

It's historic, with Java inheriting that syntax from C.

The x is necessary to distinguish 0377 (for example) from 0x377, where the leading zero in the former indicates that it's in octal (base 8):

   377 - base 10
  0377 - base 8
 0x377 - base 16
Alnitak
  • 334,560
  • 70
  • 407
  • 495
1

See the JLS:

In a hexadecimal or binary literal, the integer is only denoted by the digits after the 0x or 0b characters and before any type suffix. Therefore, underscores may not appear immediately after 0x or 0b, or after the last digit in the numeral.

It's used in order to distinguish between bases.

Maroun
  • 94,125
  • 30
  • 188
  • 241