[Home] [Downloads] [Search] [Help/forum]



Convert a string into a number


n = bit.tonumber (s, base)


This takes a string, and converts it into a number. Unlike the standard Lua tonumber function this function will handle up to a 52-bit number (the default Lua number conversion will only go to 32-bit numbers).


print (bit.tonumber ("A7C5AC471", 16)) -->  45035996273

The base is optional and defaults to 10. The base can be in the range 2 to 36. Fractional numbers are not supported, nor are numbers with exponents (eg. 10.24e15). For such numbers use the standard Lua "tonumber" function.

Because of limitations in the size of a floating point number, the maximum string value that can be converted is a 52 bit number, ie: hex FFFFFFFFFFFFF (decimal 4503599627370495).

Leading whitespace is skipped. After that, there can be an optional + or - sign.

See Also ...

Lua functions

bit.ashr - Shifts a number right, preserving sign
bit.band - Bitwise 'and'
bit.bor - Bitwise 'or'
bit.clear - Clears one or more bits
bit.mod - Bitwise 'modulus' (remainder after integer divide)
bit.neg - Bitwise 'negate' (ones complement)
bit.shl - Shifts a number left
bit.shr - Shifts a number right
bit.test - Bitwise 'test'
bit.tostring - Convert a number into a string
bit.xor - Bitwise 'exclusive or'


Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua LPEG library
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting callbacks - plugins

(Help topic: lua=bit.tonumber)

Documentation contents page

Search ...

Enter a search string to find matching documentation.

Search for:   

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]