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

相关推荐
花褪残红青杏小15 小时前
Rust图像处理第6节- 均值模糊 & 中值模糊:3×3 邻域的两种经典玩法
rust·webassembly·图形学
子兮曰19 小时前
前端工具链的「Rust 化」:一场没有赢家的军备竞赛?
前端·后端·rust
星栈21 小时前
写 Dioxus Demo 不难,难的是把它写成项目
前端·rust·前端框架
mCell1 天前
【锐评】桌面端技术营销:别拿跑分当工程判断
前端·rust·electron
武子康1 天前
调查研究-201 Rust 里的 dev build 和 release build:为什么同一份代码性能差这么多?
后端·架构·rust
doiito1 天前
【Agent Harness】Gliding Horse 的 L2 作战地图:让多 Agent 协作从“摸黑”变成“透明”
ai·rust·架构设计·系统设计·ai agent
星栈2 天前
我用 Rust + Dioxus 做了个全栈跨平台笔记应用:再把新建、编辑和交付补上
前端·rust·前端框架
独孤留白2 天前
从C到Rust:基本类型 C 的隐式不确定 vs Rust 的显式确定
rust
清晨很温柔啊2 天前
# 用 Rust 手搓 AI 自演化主板:当 18 个异构器官长出 C++ 骨骼
rust
星栈3 天前
我用 Rust + Dioxus 做了个全栈跨平台笔记应用:第一版先把列表和详情跑通
前端·rust·前端框架