目录
HEX与字符串互转
节流计划旨在降低物联网产品的硬件成本,本专栏将在2021年持续更新。
节流计划不采用STM32/STC/PIC等传统MCU,而是充分挖掘AIR724UG芯片除4G通信外的其他功能,包含但不限于GPIO、TTS、LCD、OTA、FLASH、ADC,KEY,UART,I2C、SPI等。
版权所有,谢绝转载,侵权必究。承接相关物联网项目合作,邮箱:realiot@163.com。
HEX与字符串互转
场景示例:
- 将HEX字符转为字符串打印
- 按照指定的格式发送数据给服务器端或串口端
- 解析服务器端或串口端发送的数据内容
应用方法:使用string.fromHex函数实现字符串→HEX数组,使用string.toHex函数实现HEX数组→字符串。
local str_buf= "1122334455667788"
local hex_buf= string.fromHex(str_buf) --将str_buf转化为了HEX数组,赋值给hex_buf
--此时hex_buf={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88}
local hex_buf={0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88}
local str_buf= string.toHex(str_buf) --将str_buf转化为了HEX数组,赋值给hex_buf
--此时str_buf="1122334455667788"
function bit_or(a, b)
local p, c = 1, 0
while a + b > 0 do
local ra, rb = a % 2, b % 2
if ra + rb > 0 then
c = c + p
end
a, b, p = (a - ra) / 2, (b - rb) / 2, p * 2
end
return c
end
function bit_not(n)
local p, c = 1, 0
while n > 0 do
local r = n % 2
if r < 1 then
c = c + p
end
n, p = (n - r) / 2, p * 2
end
return c
end
function bit_and(a, b)
local p, c = 1, 0
while a > 0 and b > 0 do
local ra, rb = a % 2, b % 2
if ra + rb > 1 then
c = c + p
end
a, b, p = (a - ra) / 2, (b - rb) / 2, p * 2
end
return c
end
function bit_xor(a, b)
local tmp1 = a
local tmp2 = b
local str = ""
repeat
local s1 = tmp1 % 2
local s2 = tmp2 % 2
if s1 == s2 then
str = "0" .. str
else
str = "1" .. str
end
tmp1 = math.modf(tmp1 / 2)
tmp2 = math.modf(tmp2 / 2)
until (tmp1 == 0 and tmp2 == 0)
return tonumber(str, 2)
end
--将一个字节的HEX数字转化为对应的两位的字符串--hex2chr_byte(50)->"32"
function hex2chr_byte(hexbyte)
if hexbyte < 256 then
local chrbyte = ""
local hex_map = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "0"}
local v1, v2 = math.floor(hexbyte / 16), hexbyte % 16
for k, v in pairs(hex_map) do
if v1 == k % 16 then
chrbyte = chrbyte .. hex_map[k]
break
end
end
for k, v in pairs(hex_map) do
if v2 == k % 16 then
chrbyte = chrbyte .. hex_map[k]
break
end
end
return chrbyte
else
log.error("ERROR", "Input value is uncorrect" .. hexbyte)
return "00"
end
end
--功能 将两位的字符串转化为对应的一个字节的数字 --chr2hex_byte("32") ->50
function chr2hex_byte(chrbyte)
if #chrbyte == 2 then
return (tonumber("0x" .. chrbyte))
else
log.error("ERROR", "Input value is uncorrect")
end
end
-- 将Lua字符串转成16进制数组,附带数组元素个数--"AA55"->{ 170,85}-- chrstr2dectable("AA55") -> 2 {0XAA,0X55}
function chr_str2hex_table(chr_str)
local hex_table = {}
for i = 1, #chr_str / 2 do
hex_table[i] = tonumber(string.sub(chr_str, i * 2 - 1, i * 2), 16)
end
return #hex_table, hex_table
end
--- 将16进制数组转成Lua字符串,附带字符长度--{ 170,85}_->"AA55"--hextable2chrstr({0xAA,0X55})->"AA55"
function hex_table2chr_str(hextable)
local chrstr = ""
for k, v in pairs(hextable) do
chrstr = chrstr .. hex2chr_byte(v)
end
return #chrstr, chrstr
end
-- XOR校验,返回数组xor校验的结果正确与否和xor校验值--table_check_xor({0xAA,0x55,0xF3})-> false,12,"0C"
function str_table_xor(str, n)
assert(type(str) == "string", "The first argument is not a string!")
if type(str) == "string" then
local xor = 0
local l, tab = chr_str2hex_table(str)
for i = 1, #tab - n do
xor = bit_xor(xor, tab[i])
end
return xor == 0x00, xor, hex2chr_byte(xor)
end
end
-- XOR校验,返回数组xor校验的结果正确与否和xor校验值--table_check_xor({0xAA,0x55,0xF3})-> false,12,"0C"
function hex_table_xor(tab, n)
assert(type(tab) == "table", "The first argument is not a table!")
if type(tab) == "table" then
local xor = 0
for i = 1, #tab - n do
xor = bit_xor(xor, tab[i])
end
return xor == 0x00, xor, hex2chr_byte(xor)
end
end
--将一个数v取k位小数
function myfloor(v, k)
if k == 1 then
return math.floor(v * 10 + 0.5) / 10
end
if k == 2 then
return math.floor(v * 100 + 0.5) / 100
end
if k == 3 then
return math.floor(v * 1000 + 0.5) / 1000
end
end
|