Solidity 中的`bytes`

在 Solidity 中,bytesbytes32 都是用来保存二进制数据 的类型,但它们的长度、使用场景、Gas 成本完全不同。


✅ 一句话区分

类型 一句话总结
bytes32 定长 32 字节,适合做哈希、地址、标识符等固定长度数据。
bytes 动态长度字节数组,适合任意长度数据(如字符串、序列化数据)。

📌 对比表

特性 bytes32 bytes (bytes memory / bytes calldata)
长度 固定 32 字节 (256 位) 动态长度,可以是 0~任意长度
存储位置 可作为 storage / memory / calldata 多为 memorycalldata(不能是 storage 直接存储)
Gas 成本 极便宜(固定大小) 随长度线性增加(32 字节为一个 slot)
可变性 不可变(定长) 可变(动态数组)
常见用途 哈希值、地址、UUID、枚举键 UTF-8 字符串、序列化数据、签名、IPFS 哈希
与字符串互转 bytes32 <=> string 需手动转换 bytesstring 可强制转换

✅ 代码示例

1. bytes32 用法
solidity 复制代码
bytes32 public constant ROLE_ADMIN = keccak256("ROLE_ADMIN");

function getHash(string memory text) public pure returns (bytes32) {
    return keccak256(abi.encodePacked(text)); // 返回 bytes32
}
2. bytes 用法
solidity 复制代码
function concat(string memory a, string memory b)
    public
    pure
    returns (string memory)
{
    return string(abi.encodePacked(a, b)); // 先转 bytes 再转 string
}
3. 二者互转
solidity 复制代码
// bytes32 -> bytes
bytes32 data = keccak256("hello");
bytes memory b = abi.encodePacked(data);

// bytes -> bytes32(必须保证长度 ≤ 32)
bytes memory b = "hello";
bytes32 data;
assembly {
    data := mload(add(b, 32)) // 手动加载前 32 字节
}

✅ 何时用哪个?

场景 推荐类型
哈希值 (如 keccak256 结果) bytes32
地址 (如 address 转 bytes) bytes32
UTF-8 字符串(不定长) stringbytes
ABI 编码数据 bytes memory
签名数据(>= 65 字节) bytes

✅ 一句话总结

  • bytes32:定长、省 gas,存哈希、存标识符
  • bytes:动态长度,存任意二进制或字符串数据
相关推荐
imaol124 分钟前
文件编程--标准IO
linux·运维·算法
青 春 记 忆28 分钟前
LeetCode 121. 买卖股票的最佳时机|Python 解法详解
python·算法·leetcode
stolentime40 分钟前
AT_agc066_a [AGC066A] Adjacent Difference题解
c++·算法·贪心算法·构造
磐链科技40 分钟前
交易所开发新范式:从社交钱包隐私保护中汲取的五大架构启示
架构·区块链
渣波1 小时前
深度解析:LeetCode 51. N 皇后 —— 回溯算法的巅峰之作
算法
咪饭只吃一小碗1 小时前
# C++ STL 常用容器方法全面总结vector / map / set / unordered_map / unordered_set
算法
daad7771 小时前
802.11 前导码与 STF 深度解析(含检测算法与 5G 对比
人工智能·算法·5g·wifi·802.11·前导码
Navigator_Z2 小时前
LeetCode //C - 1203. Sort Items by Groups Respecting Dependencies
c语言·算法·leetcode
Chasing__Dreams3 小时前
Redis--基础知识点--33--String Hash List Set ZSet
redis·list·哈希算法