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]);

相关推荐
大卫小东(Sheldon)1 小时前
GIM 1.5发布了! 支持Windows系统了
git·ai·rust
寻月隐君2 小时前
告别竞态条件:基于 Axum 和 Serde 的 Rust 并发状态管理最佳实践
后端·rust·github
成遇10 天前
在Vscode中安装Sass并配置
vscode·rust·sass
止观止10 天前
Rust智能指针演进:从堆分配到零复制的内存管理艺术
开发语言·后端·rust
学無芷境10 天前
Cargo 与 Rust 项目
开发语言·后端·rust
Python私教10 天前
源滚滚AI编程:我为什么选择Rust开发博客项目
开发语言·rust·ai编程
机构师10 天前
<tauri><threejs><rust><GUI>基于tauri和threejs,实现一个3D图形浏览程序
javascript·后端·rust
寻月隐君10 天前
用 Rust 打造微型游戏:从零开始的 Flappy Dragon 开发之旅
后端·rust·github
Andrew_Ryan10 天前
rust read 100G large file
rust