我对lua和一般编程(自学成才)相对较新,所以请保持温和!
无论如何,我写了一个lua脚本来读取游戏中的UDP消息。消息的结构是:
DATAxXXXXaaaaBBBBccccDDDDeeeeFFFFggggHHHH
DATAx = 4 letter ID and x = control character
XXXX = integer shows the group of the data (groups are known)
aaaa...HHHHH = 8 single-precision floating point numbers
最后一个是我需要解码的数字。
如果我按收到的方式打印消息,则类似于:
DATA*{V???A?A?...etc.
使用string.byte(),我得到像这样的字节流(我已经“格式化”字节以反映上面的结构。
68 65 84 65/42/20 0 0 0/237 222 28 66/189 59 182 65/107 42 41 65/33 173 79 63/0 0 128 63/146 41 41 65/0 0 30 66/0 0 184 65
前5个字节当然是DATA *。接下来的4个是第20组数据。接下来的字节,我需要解码的那些字节,并且等于这些值:
237 222 28 66 = 39.218
189 59 182 65 = 22.779
107 42 41 65 = 10.573
33 173 79 63 = 0.8114
0 0 128 63 = 1.0000
146 41 41 65 = 10.573
0 0 30 66 = 39.500
0 0 184 65 = 23.000
我找到了使用BitConverter.ToSingle()进行解码的C#代码,但我还没有找到任何类似Lua的代码。任何的想法?
你有什么Lua版本? 此代码适用于Lua 5.3
local str = "DATA*\20\0\0\0\237\222\28\66\189\59\182\65..."
-- Read two float values starting from position 10 in the string
print(string.unpack("<ff", str, 10)) --> 39.217700958252 22.779169082642 18
-- 18 (third returned value) is the next position in the string
对于Lua 5.1,你必须编写特殊功能(或从François Perrad's git repo窃取它)
local function binary_to_float(str, pos)
local b1, b2, b3, b4 = str:byte(pos, pos+3)
local sign = b4 > 0x7F and -1 or 1
local expo = (b4 % 0x80) * 2 + math.floor(b3 / 0x80)
local mant = ((b3 % 0x80) * 0x100 + b2) * 0x100 + b1
local n
if mant + expo == 0 then
n = sign * 0.0
elseif expo == 0xFF then
n = (mant == 0 and sign or 0) / 0
else
n = sign * (1 + mant / 0x800000) * 2.0^(expo - 0x7F)
end
return n
end
local str = "DATA*\20\0\0\0\237\222\28\66\189\59\182\65..."
print(binary_to_float(str, 10)) --> 39.217700958252
print(binary_to_float(str, 14)) --> 22.779169082642
它是IEEE-754单精度二进制文件的little-endian字节顺序:
例如,0 0 128 63
是:
00111111 10000000 00000000 00000000 (63) (128) (0) (0)
为什么等于1
要求你理解IEEE-754表示的基础知识,即它使用指数和尾数。请参阅here开始。
有关如何在Lua 5.3中使用string.unpack()
以及可以在早期版本中使用的一种可能实现,请参阅@ Egor上面的答案。