如何将十六进制数字字符串转换为它在Lua中表示的值 [英] How do I convert string of hex digits to value it represents in Lua

查看:205
本文介绍了如何将十六进制数字字符串转换为它在Lua中表示的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读大量的十六进制数据。它们以字符串的形式出现,我解析了它们的line_codes,它告诉我如何处理其余的数据。一行设置地址(MSW)的最重要的单词,另一行设置最低位(LSW)。

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



这一切都很顺利,但我应该检查地址是介于如果地址<=007FFFh和地址> =000200h,则

 b打印我在
end

大家都知道,Lua是不是这样的粉丝,因为它使用< = > = 字符串给了我一个错误。 p>

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

解决方案您可以使用 tonumber

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

请注意, NU mbers不是十六进制的。它们也不是十进制,八进制或其他。他们只是数字。数字0xFF与255的数字相同。FF和255是相同数字的字符串表示。


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?

解决方案

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.

这篇关于如何将十六进制数字字符串转换为它在Lua中表示的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆