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

相关推荐
全栈陈序员6 小时前
基于Rust 实现的豆瓣电影 Top250 爬虫项目
开发语言·爬虫·rust
百锦再6 小时前
第17章 模式与匹配
开发语言·后端·python·rust·django·内存·抽象
JosieBook8 小时前
【Rust】基于Rust + WebAssembly;实现人机记忆井字棋游戏(人机对战)
游戏·rust·wasm
万事可爱^12 小时前
GitHub爆火开源项目——RustScan深度拆解
c语言·开发语言·rust·开源·github·rustscan
受之以蒙18 小时前
具身智能的“任督二脉”:用 Rust ndarray 打通数据闭环的最后一公里
人工智能·笔记·rust
有梦想的攻城狮18 小时前
初识Rust语言
java·开发语言·rust
JosieBook1 天前
【Rust】基于Rust 设计开发nginx运行日志高效分析工具
服务器·网络·rust
四问四不知1 天前
Rust语言入门
开发语言·rust
JosieBook1 天前
【Rust】 基于Rust 从零构建一个本地 RSS 阅读器
开发语言·后端·rust
云边有个稻草人1 天前
部分移动(Partial Move)的使用场景:Rust 所有权拆分的精细化实践
开发语言·算法·rust