Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain to me where the 'x' in hexadecimal '0xf0' comes from [duplicate]

Tags:

java

hex

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.

like image 258
seamus Avatar asked Feb 13 '14 08:02

seamus


2 Answers

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
like image 162
Alnitak Avatar answered Oct 11 '22 08:10

Alnitak


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.

like image 26
Maroun Avatar answered Oct 11 '22 07:10

Maroun