简体   繁体   English

如何将十六进制数字的字符串转换为它在Lua中表示的值

[英]How do I convert string of hex digits to value it represents in Lua

I'm reading in a lot of lines of hex data. 我正在阅读很多十六进制数据行。 They come in as strings and I parse them for line_codes which tell me what to do with the rest of the data. 它们以字符串形式出现,我将它们解析为line_codes,告诉我如何处理其余数据。 One line sets a most significant word of an address (MSW), another line sets the least significant (LSW). 一行设置地址的最重要字(MSW),另一行设置最低有效字(LSW)。

I then need to concatenate those together such that if MSW = "00ff" and LSW = "f10a" address would be 00fff10a. 然后我需要将它们连接在一起,这样如果MSW =“00ff”并且LSW =“f10a”地址将是00fff10a。

This all went fine, but then I was supposed to check if address was between a certain set of values: 这一切都很好,但后来我应该检查地址是否在某组值之间:

if address <= "007FFFh" and address >= "000200h" then
    print "I'm in"
end

As you all probably know, Lua is not a fan of this as it gives me an error using <= and >= with strings. 正如你们大家都知道的那样,Lua并不喜欢这个,因为它使用<=>=和字符串给我一个错误。

If there a way I can convert the string into hex, such that "FFFF" would become 0xFFFF? 如果有一种方法我可以将字符串转换为十六进制,这样“FFFF”将变为0xFFFF?

You use tonumber : 你使用tonumber

local someHexString = "03FFACB"
local someNumber = tonumber(someHexString, 16)

Note that numbers are not in hexadecimal. 请注意,数字不是十六进制。 Nor are they in decimal, octal, or anything else. 它们也不是十进制,八进制或其他任何东西。 They're just numbers. 他们只是数字。 The number 0xFF is the same number as 255. "FF" and "255" are string representations of the same number. 数字0xFF与255的数字相同。“FF”和“255”是相同数字的字符串表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM