Gourav Das
Gourav Das

Reputation: 49

WHAT is the output of print(0x1e1) in Lua? And Why?

print(0x1e1) will print 481 in Lua, but I don't know why. Can anyone please help me understand?

Upvotes: 0

Views: 585

Answers (1)

arboreal84
arboreal84

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.

Formatting

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

Related Questions