lua-users home
lua-l archive

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


On 8/17/2012 11:29 AM, Edward Scerri wrote:
Dear All,

I am trying to output a numeric value using in a 4digit hexadecimal
format in the following way:
" io.write(string.format("%04x", printchars)) "

I am getting an error as follows:

./pulse_parser.lua 2012-08-16
/usr/bin/lua: ./pulse_parser.lua:16: bad argument #2 to 'format'
(integer expected, got number)

This, in combination with

Copyright (C) 1994-2008 Lua.org, PUC-Rio (double int32)"
.                                          ^^^^^^^^^^^^^^^

makes me think you're using the LNUM patch, which introduces a difference between integers and floating point. In this case, the string.format expects an integer, but it got a floating point number.

 AFAIK, there area two options:

1) Don't use Lua with LNUM patch; it is mostly compatible but not entirely so

2) Try to explicitly convert the number to an integer. I haven't used the LNUM patch much, but ISTR you can "cast" a number to integer by doing something like

 tonumber(math.floor( printchars + .5))

as 'tonumber' will convert a floating point number which can be represented by an integer to an integer.