I'm doing some binary operations which are often shown as hex-es. I have seen both the 0x
and \x
as prefixes.
In which case is which used?
If you print a hexadecimal number, Python uses the prefix '0x' to indicate that it's a number in the hexadecimal system and not in the decimal system like normal integers.
The prefix we use for hexadecimal is "0x". To represent the numbers 0-9, we simply use those digits. To represent 10-15, we use the letters A-F. A.
The prefix 0x is used in code to indicate that the number is being written in hex.
Since Python returns a string hexadecimal value from hex() we can use string. replace to remove the 0x characters regardless of their position in the string (which is important since this differs for positive and negative numbers).
0x
is used for literal numbers. "\x"
is used inside strings to represent a character
>>> 0x41
65
>>> "\x41"
'A'
>>> "\x01" # a non printable character
'\x01'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With