Codectionary / Developer documentation / Lua

Lua numeric base conversion

tonumber accepts a base from 2 through 36 for integer text. Invalid text returns nil, which must be distinguished from a valid zero.

Syntax

local value = tonumber(text, base)

Examples

Lua numeric base conversion

Run with standalone Lua 5.4: lua example.lua. These scripts target Lua 5.4 rather than Roblox Luau.

print(tonumber("FF", 16), tonumber("1010", 2))
print(tonumber("102", 2) == nil)
  1. Output: 255 and 10, then true. Digit 2 is invalid in base 2.

Best practices

  • Check nil before arithmetic and validate ranges separately. Successful numeric conversion does not prove a value is appropriate for the application.

Runtime & versions

Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Lua reference manuals

Specifications & further reading