如何将十六进制数字字符串转换为它在 Lua 中表示的值
我正在读很多行十六进制数据。它们以字符串形式出现,我将它们解析为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用
tonumber
:请注意,数字不是十六进制的。它们也不是十进制、八进制或其他任何形式。它们只是数字。数字 0xFF 与 255 是相同的数字。“FF”和“255”是相同数字的字符串表示形式。
You use
tonumber
: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.