IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> LUAT编程技巧总结 -> 正文阅读

[嵌入式]LUAT编程技巧总结

目录

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

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2021-08-19 12:12:44  更:2021-08-19 12:14:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 0:18:26-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码