lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



Dimitris P. wrote:
I generally often wonder why lua doesn't have some helpful features C has like
> the one above or support for specifying numeric literals in non-decimal bases
> like 2, 8, 16

The tonumber function can be used to convert numbers from different bases to base ten.

$ lua
Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
>
> function hex(n) return tonumber(n,16) end
>
> = hex'100'
256
>

If you really want to use the 0x prefix like C, here is a patch.

http://lua-users.org/files/wiki_insecure/users/pshook/lua-5.0-hex.patch

$ lua
Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
>
> = 0x100
256
> = 0x1000
4096
>

- Peter Shook