In computer science and implicitly in Ethereum and Solidity, the 0x prefix serves the fundamental purpose of signifying that the following data is in hexadecimal (base-16).
Why is it used?
Without a prefix, many hexadecimal numbers would be
indistinguishable from decimal numbers.
- Scenario: You see the number 80.
- Problem: Is this "eighty" (decimal) or is it 8 * 16^1 + 0 = 128 (hexadecimal)?
- Solution: By writing 0x80, the ambiguity is removed. The compiler immediately knows the base is 16.
In Ethereum and Solidity 0x is used for different types of data like addresses, opcodes, encoded data and hashes.
The Exception is hex Strings, where hex is a special keyword in Solidity for long strings of raw data. In this format, you do not use the 0x prefix inside the quotes.
Syntax: hex"..." or hex'...'
Top comments (0)