在工作过程中,遇到了下面的问题:
串口屏通过自定义串口指令,读取到了一个16进制的浮点数 但是其分成了4段储存在了通讯缓存中。CC DD AA BB
使用了一个非常笨的方法硬跑出来
思路就是用string.pack 将其拼接为0xAABBCCDD 然后通过string.unpack函数解码为10进制数
function HexToFloat(a,b,c,d)
local temp='0x'
local tempa=math.floor(a/16) --因为读取到的是10进制数,所以要进行处理
if tempa==10 then temp=temp..'a'
elseif tempa==11 then temp=temp..'b'
elseif tempa==12 then temp=temp..'c'
elseif tempa==13 then temp=temp..'d'
elseif tempa==14 then temp=temp..'e'
elseif tempa==15 then temp=temp..'f'
else temp=temp..tempa
end
tempa=a%16
if tempa==10 then temp=temp..'a'
elseif tempa==11 then temp=temp..'b'
elseif tempa==12 then temp