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

发布于 2024-11-30 22:26:53 字数 463 浏览 0 评论 0原文

我正在读很多行十六进制数据。它们以字符串形式出现,我将它们解析为 line_codes,它告诉我如何处理其余数据。一行设置地址的最高有效字 (MSW),另一行设置最低有效字 (LSW)。

然后我需要将它们连接在一起,这样如果 MSW =“00ff”且 LSW =“f10a” 地址将为 00fff10a。

这一切都很顺利,但后来我应该检查地址是否在一组特定的值之间:

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

正如你们可能知道的那样,Lua 不喜欢这个,因为它使用 <= 和 >= 带有字符串。

如果有办法可以将字符串转换为十六进制,这样“FFFF”就会变成0xFFFF?

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. One line sets a most significant word of an address (MSW), another line sets the least significant (LSW).

I then need to concatenate those together such that if MSW = "00ff" and LSW = "f10a"
address would be 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.

If there a way I can convert the string into hex, such that "FFFF" would become 0xFFFF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2024-12-07 22:26:53

您使用tonumber

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

请注意,数字不是十六进制的。它们也不是十进制、八进制或其他任何形式。它们只是数字。数字 0xFF 与 255 是相同的数字。“FF”和“255”是相同数字的字符串表示形式。

You use 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文