user123454321
user123454321

Reputation: 152

How can I save hexa number into a variable in lua?

How can I save hexa number into a variable in lua (as number and not as string) ?
I tried that:

Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> vendor_rand=259754994c07a716565e459a64f2a8672c1956151249d8eaeb3f7f1c6513e0e1
stdin:1: malformed number near '259754994c07a716565e459a64f2a8672c1956151249d8eaeb3f7f1c6513e0e1'

but I got here an error.
I saw this option tonumber("fe",16) but it save it as a string and not as a number type.

Upvotes: 0

Views: 110

Answers (2)

Zakk
Zakk

Reputation: 2063

You are getting this error because 259754994c07... is not syntactically valid code. You have to use the 0x prefix to write a number in hex format:

vendor_rand = 0x259754994c07a716565e459a64f2a8672c1956151249d8eaeb3f7f1c6513e0e1

However, this number is too big for Lua. On versions prior to 5.3, you will have a double precision loss (since Lua converts it to a double). On 5.3 onwards, you will have an integer overflow. For 32-bit systems, the maximum value an integer can hold is 2^31 - 1, for 64-bit 2^63 - 1.

At the time of writing this answer, Lua 5.1.5 is old. The latest version is 5.4.4 and is recommended to use unless you are required to use an older version.

EDIT: Thanks @LMD for details.

Upvotes: -1

Piglet
Piglet

Reputation: 28964

local hex = "259754994c07a716565e459a64f2a8672c1956151249d8eaeb3f7f1c6513e0e1"

There is no numeric type that could hold 32 bytes. Also there is no mathematical operation you could do with it that would make any sense. So just use a string to store that number.

Upvotes: 2

Related Questions