Rust常用库之处理hex数据hex-literal

文章目录

Rust常用库之处理hex数据hex-literal

需求

十六进制数据解码为raw data(vec)、将字节序列转换为十六进制表示

hex-literal

官方:https://docs.rs/releases/search?query=hex_literal

This crate provides the hex! macro for converting hexadecimal string literals to a byte array at compile time.

It accepts the following characters in the input string:

  • '0'...'9', 'a'...'f', 'A'...'F' --- hex characters which will be used in construction of the output byte array
  • ' ', '\r', '\n', '\t' --- formatting characters which will be ignored

Examples

const DATA: u8; 4 = hex!("01020304");

assert_eq!(DATA, 1, 2, 3, 4);

assert_eq!(hex!("a1 b2 c3 d4"), 0xA1, 0xB2, 0xC3, 0xD4);

assert_eq!(hex!("E5 E6 90 92"), 0xE5, 0xE6, 0x90, 0x92);

assert_eq!(hex!("0a0B 0C0d"), 10, 11, 12, 13);

let bytes = hex!("

00010203 04050607

08090a0b 0c0d0e0f

");

assert_eq!(bytes, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);

assert_eq!(hex!("0a0B // 0c0d line comments"), 10, 11);

assert_eq!(hex!("0a0B // line comments

0c0d"), 10, 11, 12, 13);

assert_eq!(hex!("0a0B /* block comments / 0c0d"), 10, 11, 12, 13);
assert_eq!(hex!("0a0B /
multi-line

block comments

*/ 0c0d"), 10, 11, 12, 13);

相关推荐
孙启超9 小时前
【AI开发之Rust】第 19 课:UniFFI 导出核心能力
开发语言·后端·rust
孙启超12 小时前
【AI开发之Rust】第 17 课:项目总览与核心架构 —— AI 助手 Rust 核心从 0 到 1
开发语言·后端·rust
柯南466813 小时前
【AI开发之Rust】第 21 课:双端集成与出包 —— Android(.so→AAR)与 iOS(xcframework)
rust·编程语言
Amos_Web13 小时前
Rspack 源码解析(十六):多类型资源如何进入 Compilation.assets
前端·rust·前端框架
Norna13 小时前
时间轮里那行凭空出现的 +1
rust
隔窗听雨眠17 小时前
FunProxy用Rust构建跨平台全链路测试抓包代理工具
开发语言·后端·rust
geovindu17 小时前
rust: search
开发语言·后端·rust
孙启超19 小时前
【AI开发之Rust】第 20 课:壳侧 API 封装 —— 把 Rust 能力接进真实 UI
开发语言·后端·rust
柯南46681 天前
【AI开发之Rust】第 20 课:壳侧 API 封装 —— 把 Rust 能力接进真实 UI
rust·编程语言