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

相关推荐
mit6.82421 小时前
并发协调的代价
rust
不爱学英文的码字机器1 天前
[鸿蒙PC命令行移植适配]移植rust三方库sd到鸿蒙PC的完整实践
华为·rust·harmonyos
星栈1 天前
Makepad 不只是画界面:事件、状态和组件通信,到底怎么写
前端·rust
绍磊leo1 天前
Tauri 2.x 教程系列 (二):React 组件化与 Tauri 命令系统
rust·tauri
日火1 天前
Are Mutexes Slow——互斥锁真的慢吗?
性能优化·rust
禁默1 天前
[鸿蒙PC命令行移植适配]移植rust三方库eza到鸿蒙PC的完整实践
华为·rust·harmonyos
绍磊leo1 天前
Tauri 2.x 教程系列 (一):Hello Tauri — 从零搭建第一个桌面应用
rust·tauri
smallswan1 天前
第十七 位运算
rust
a诠释淡然2 天前
C++ vs Rust:哪个更适合你的下一个项目?
开发语言·c++·rust
Vallelonga2 天前
Rust 生命周期标注积累
开发语言·rust