Reputation: 49
print(0x1e1)
will print 481 in Lua, but I don't know why. Can anyone please help me understand?
Upvotes: 0
Views: 585
Reputation: 2154
Because 481 (decimal) is 1e1 (hexadecimal).
The 0x
prefix means the number is hexadecimal, or base 16.
No prefix means the number is decimal, or base 10.
print
will format numbers as decimal by default.
To print numbers in a specific base:
# As decimal
print(string.format("%d", 0x1e1)) # Output: 481
print(0x1e1) # Output: 481
# As hexadecimal
print(string.format("%x", 0x1e1)) # Output: 1e1
Upvotes: 5