7 个版本
0.1.6 | 2021 年 1 月 11 日 |
---|---|
0.1.5 | 2019 年 11 月 28 日 |
0.1.3 | 2019 年 10 月 18 日 |
#828 在 数据结构
16KB
249 行
Big_Unsigned_Ints
描述
Big_Unsigned_Ints 是一个 Rust 包,允许您使用从 U256 到 U2048 的大型无符号整数。这些通过固定大小的数组实现,并将 u8 转换为 u64 类型。它还允许您不使用 unsafe 代码将 u64 转换回 u8 数组。
u8 -> u64 转换使用 mem::transmute 与 unsafe 代码执行此过程。
除了标准 traits From 和 Into 以外,不应使用任何其他函数来在 u8 和 u64 之间转换。
如何使用
请在您的 cargo.toml 文件中的依赖项下写入以下内容
big_unsigned_ints= "0.1.6"
然后,您可以如此导入它
extern crate big_unsigned_ints;
请查看测试文件夹以了解集成测试以及如何使用库本身。
使用标准的文档说明
将字节数组转换为 U256 类型
fn example_bytes (){
// 32-fixed sized array of bytes
let x = [243u8;32];
// Conversion Occurs From Bytes of Array To The Type
let y = big_unsigned_ints::U256::from(x);
// Prints as Hexadecimal
println!("{}",y)
}
将字节数组转换回 U256 类型后转换为一个字节数组
fn bytes_to_big_int (){
// Create an array of 32x 243u8, or 256bits
let x = [243u8;32];
// Convert From the Array of Bytes Into a U256 Type ([u64;4])
let y = big_unsigned_ints::U256::from(x);
// Convert Back Into An Array of Bytes Specifying the Type For Bytes
let b: [u8;32] = y.into();
}
可用类型
-
U256
[u64;4]
|[u8;32]
-
U384
[u64;6]
|[u8;48]
-
U512
[u64;8]
|[u8;64]
-
U1024
[u64;16]
|[u8;128]
-
U2048
[u64;32]
|[u8;256]
许可证
-
MIT许可证
-
Apache 2.0