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

相关推荐
铅笔侠_小龙虾4 小时前
rust 学习(9):结构体
开发语言·学习·rust
铅笔侠_小龙虾6 小时前
rust 学习(12):常用集合(上)-- Vec
rust
落知秋1 天前
RUST中的trait是什么?
笔记·rust
Embedded-Xin1 天前
中间件—zenoh零基础入门
linux·中间件·rust·机器人·自动驾驶·嵌入式
程序员爱钓鱼1 天前
Rust 可变借用详解:&mut T 与安全修改数据
后端·面试·rust
SomeB1oody2 天前
【RustyML入门】2.9. MeanShift
开发语言·后端·机器学习·rust·教程
SomeB1oody2 天前
【RustyML入门】2.10. 主成分分析
开发语言·后端·机器学习·rust·教程
yushikong2 天前
关于rust开发ch32x033f8p6的一些记录
开发语言·后端·rust
程序员爱钓鱼2 天前
Rust Borrow借用详解:不转移所有权访问数据
后端·面试·rust