solidity - 我想找到 bytes32 数据的子字符串(索引 0 到 13)并将其转换为 uint256 数据
问题描述
我被困在坚固的代码中。我想要以下数据的子字符串并将其转换为 uint256 数据
bytes32 hmacSha256 = 0xf83bf40815929b2448b230d51fa2eaa5b8ccffd87691db7e62bf817b2cbb56ad;
我想要上面 hmacSha256 数据的前 13 个字符,即“f83bf40815929”及其 uint256,即。4366982094870825。我尝试了很多东西,但都失败了。代码和尝试过的代码如下:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Storage
* @dev Store & retrieve value in a variable
*/
contract TestContract {
function getsubstring1() public pure returns (string memory, uint256, string memory) {
bytes32 hmacSha256 = 0xf83bf40815929b2448b230d51fa2eaa5b8ccffd87691db7e62bf817b2cbb56ad;
bytes memory tempH = bytes(abi.encodePacked(hmacSha256));
uint256 uintHash = uint256(hmacSha256);
//I want below values as result.
// _hs is first 13 chars of hmacSha256.
//neither i could derive the _hs and nor _h from _hs.
string memory _hs = '0xf83bf40815929';
uint256 _h = 0xf83bf40815929;
//What I tried
//bytes13 _hs1 = bytes13(hmacSha256);
//but its returning 0xf83bf40815929b2448b230d51f which is double the length of expected value 0xf83bf40815929
//string memory _hs1 = substring(string(abi.encodePacked(hmacSha256)), 0, 13);
//above code is throwing error:Failed to decode output: null: invalid codepoint at offset 0; bad codepoint prefix
// bytes memory _hs2 = tempH[0:13] ;
//above giving error that its for bytes calldata dynamic array
//uint256 _h1 = uint256(bytes(abi.encodePacked(_hs)));
return (_hs, _h, _hs1);
}
function substring(string memory str, uint startIndex, uint endIndex) public pure returns (string memory) {
bytes memory strBytes = bytes(str);
bytes memory result = new bytes(endIndex-startIndex);
for(uint i = startIndex; i < endIndex; i++) {
result[i-startIndex] = strBytes[i];
}
return string(result);
}
}
解决方案
您需要执行一些类型转换和按位运算,请参阅代码注释。
代码:
pragma solidity ^0.8;
contract TestContract {
function getsubstring() external pure returns (bytes7, uint256) {
bytes32 hmacSha256 = 0xf83bf40815929b2448b230d51fa2eaa5b8ccffd87691db7e62bf817b2cbb56ad;
bytes7 first7Bytes = bytes7(hmacSha256); // get the first 7 bytes (14 hex characters): 0xf83bf40815929b
bytes7 thirteenHexCharacters = first7Bytes >> 4; // move 4 bytes (1 hex character) to the right: 0x0f83bf40815929
bytes32 castBytes = bytes32(thirteenHexCharacters); // cast the bytes7 to bytes32 so that we can cast it to integer later
bytes32 castBytesMoved = castBytes >> 200; // move 200 bytes (50 hex characters) to the right: 0x000000000000000000000000000000000000000000000000000f83bf40815929
uint256 integerValue = uint256(castBytesMoved); // cast the bytes32 to uint256
return (thirteenHexCharacters, integerValue);
}
}
返回值:
bytes7: 0x0f83bf40815929
uint256: 4366982094870825
推荐阅读
- mysql - Spark scala 加入限制子查询
- sql-server - 如何在 Elixir 中不使用模式的情况下从不同的表中删除注册表
- shell - 如何使用 shell 脚本在 VSCode 的集成终端中打开一个新的终端选项卡?
- javascript - ImportJSON.gs 解析嵌套的 JSON 数组
- apache-kafka - 从 Kafka 状态存储中删除记录是否也会从更改日志主题中删除记录?
- python-3.x - 如何将特定行的内容打印到控制台?
- angular - 内容安全策略:页面设置阻止在 http://localhost:4200/favicon.ico ("default-src") 加载资源
- c# - 如何检测对已知文件夹重定向的更改
- c# - 如何将参数传递给 C# .Net Core 项目?
- performance-testing - 庞巴迪 - 从文件中读取