Ask Your Question
0

Trouble converting string number to number with tonumber() function on [closed]

asked 2024-07-11 07:41:05 +0000

flok gravatar image

updated 2024-07-11 08:15:56 +0000

grahamb gravatar image

Hey guys,

i hope someone has an idea about my problem with the tonumber() function is

Problem:

I want to convert number that come from a text file that is loaded inside a dissector. On one PC with the current 4.2.6 release (also happening with 4.2.5) the basic code work, on the other one the result of the tonumber() function is nil

Test code:

local a = "-15.687"
local b = tonumber(a)
print("a " .. a)
print("b " .. b)

Below i copied the info on the about window from wireshark (both pc just got the newly release 4.2.6 installed)

About data of the PC where code works:

Version 4.2.6 (v4.2.6-0-g2acd1a854bab). 

Compiled (64-bit) using Microsoft Visual Studio 2022 (VC++ 14.37, build 32822),
with GLib 2.78.0, with Qt 6.5.3, with libpcap, with zlib 1.3.0, with PCRE2, with
Lua 5.2.4 (with UfW patches), with GnuTLS 3.8.4 and PKCS #11 support, with
Gcrypt 1.10.2-unknown, with Kerberos (MIT), with MaxMind, with nghttp2 1.61.0,
with nghttp3 1.0.0, with brotli, with LZ4, with Zstandard, with Snappy, with
libxml2 2.11.5, with libsmi 0.5.0, with QtMultimedia, with automatic updates
using WinSparkle 0.8.0, with AirPcap, with Minizip, with binary plugins.

Running on 64-bit Windows 11 (23H2), build 22631, with AMD Ryzen 9 7950X 16-Core
Processor (with SSE4.2), with 65143 MB of physical memory, with GLib 2.78.0,
with Qt 6.5.3, with Npcap version 1.79, based on libpcap version 1.10.4, with
PCRE2 10.42 2022-12-11, with c-ares 1.27.0, with GnuTLS 3.8.4, with Gcrypt
1.10.2-unknown, with nghttp2 1.61.0, with nghttp3 1.0.0, with brotli 1.0.9, with
LZ4 1.9.3, with Zstandard 1.5.2, without AirPcap, with dark display mode, with
HiDPI, with QPA plugin "windows", with LC_TYPE=English_United Kingdom.utf8,
binary plugins supported.

About of PC which this doesn't work:

Version 4.2.6 (v4.2.6-0-g2acd1a854bab).


Compiled (64-bit) using Microsoft Visual Studio 2022 (VC++ 14.37, build 32822),
with GLib 2.78.0, with Qt 6.5.3, with libpcap, with zlib 1.3.0, with PCRE2, with
Lua 5.2.4 (with UfW patches), with GnuTLS 3.8.4 and PKCS #11 support, with
Gcrypt 1.10.2-unknown, with Kerberos (MIT), with MaxMind, with nghttp2 1.61.0,
with nghttp3 1.0.0, with brotli, with LZ4, with Zstandard, with Snappy, with
libxml2 2.11.5, with libsmi 0.5.0, with QtMultimedia, with automatic updates
using WinSparkle 0.8.0, with AirPcap, with Minizip, with binary plugins.

Running on 64-bit Windows 10 (22H2), build 19045, with 13th Gen Intel(R)
Core(TM) i7-13850HX (with SSE4.2), with 65136 MB of physical memory, with GLib
2.78.0, with Qt 6.5.3, with Npcap version 1.79, based on libpcap version 1.10.4,
with PCRE2 ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by flok
close date 2024-07-12 18:38:07.611421

Comments

Working output:

a -15.687

b -15.687

Is this what you get on the non-working system?

a -15.687

[string "local a = "-15.687"..."]:4: attempt to concatenate local 'b' (a nil value)
Chuckc gravatar imageChuckc ( 2024-07-11 11:05:40 +0000 )edit

Could it be this? Non-working system has language set to LC_TYPE=German_Germany.utf8.
http://lua-users.org/Re: How to extract a floating point number locale-independantly

If running in a locale where '.' is not the decimal separator,

Chuckc gravatar imageChuckc ( 2024-07-11 11:21:51 +0000 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2024-07-12 18:34:28 +0000

André gravatar image

This should work on both PCs, with either English or German locale.
By resetting the locale to the default ("C"), thus with a dot as decimal symbol.

assert(os.setlocale'C')
local a = "-15.687"
local b = tonumber(a)
print("a " .. a)
print("b " .. b)

The opposite is also possible. By setting os.setlocale'de_DE' the decimal symbol is set to comma (among other regional settings).

edit flag offensive delete link more

Comments

Thanks for the answers guys. It was really a localization issue. I though i tested this but seems i didn't see that i was fixed or i'm just blind!

the os.setlocale("C") is the solution i'm going with

flok gravatar imageflok ( 2024-07-12 18:37:23 +0000 )edit

@flok, normally we don't close "answered questions, instead you should click the checkmark icon next to the most useful answer to let others know that was a good answer to your question,

grahamb gravatar imagegrahamb ( 2024-07-14 14:21:03 +0000 )edit

Question Tools

Stats

Asked: 2024-07-11 07:41:05 +0000

Seen: 118 times

Last updated: Jul 12