我有两个合同,主合同要求以字节32数组输入,然后主将字节32转换为地址uint 256和字节,然后调用第二个合同,这需要在地址uint 256和字节中输入
我正在尝试将此字节数据转换为32字节
“ 0x793e39cd00000000000000000064a436ae831c1672ae81f674cab8b6775df3475c0000000000000000000000004a6bc4e803c62081ffebcc8d227b5a87a58f1f8f000000000000000000000000c4375b7de8af5a38a9350000000000000000000000000000000000000000000000000000001000000b000000000000000000000000
但是当我将字节32的数据转换回字节时,我得到了
“ 0x793e39cd00000000000000000000000064a436ae831c1672ae81f674cab8b677”
要将bytes32转换为我使用的字节
abi.encodePacked()
对于将字节转换为字节32,我找到了此代码,但这不正确。
function bytesToBytes32(bytes memory source) private pure returns (bytes32 result) {
if (source.length == 0) {
return 0x0;
}
assembly {
result := mload(add(source, 32))
}
}
答案 0 :(得分:0)
如果您的目标是将bytes
转换为bytes32[]
,也许您可以尝试以下方法:
function bytesToBytes32Array(bytes memory data)
public
pure
returns (bytes32[] memory)
{
// Find 32 bytes segments nb
uint256 dataNb = data.length / 32;
// Create an array of dataNb elements
bytes32[] memory dataList = new bytes32[](dataNb);
// Start array index at 0
uint256 index = 0;
// Loop all 32 bytes segments
for (uint256 i = 32; i <= data.length; i = i + 32) {
bytes32 temp;
// Get 32 bytes from data
assembly {
temp := mload(add(data, i))
}
// Add extracted 32 bytes to list
dataList[index] = temp;
index++;
}
// Return data list
return (dataList);
}