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

相关推荐
@大迁世界11 小时前
告别 Go,拥抱 Rust
开发语言·golang·rust
低调滴开发12 小时前
3 行代码写崩一个函数?切片才是 Rust 字符串处理的正确姿势
开发语言·后端·rust
小灰灰搞电子15 小时前
Rust+Slint 实现高级动态消息提示框源码分享
开发语言·rust
qq_4069810316 小时前
大白话rust系列08 当变量遮蔽遇上引用,探究变量遮蔽的本质
开发语言·后端·rust
轻舟技术A17 小时前
Pi、rpi、Pi-Bolt 重测:rpi 在启动、内存与读取上全面领先
人工智能·rust
用户144984334647518 小时前
我为什么做一个开源的 Mac 清理工具
rust
古城小栈20 小时前
Rust 学习路线,供初学者参考
开发语言·rust
小熊吃保安2 天前
Windows也能用 Apple Watch 解锁
windows·rust·开源
离歌漠3 天前
《我用 Tauri 做了一个本地开发工具箱》
rust·typescript·vue