[英]How to use BytesToUint function in Solidity (the one with assembly)?
I was using the following function to convert bytes to uint:我正在使用以下 function 将字节转换为 uint:
function bytesToUint(bytes b) public pure returns (uint){
uint number;
for(uint i=0;i<b.length;i++){
number = number + uint(b[b.length-1-i])*(10**i);
}
return number;
}
Since explicit byte1 to uint conversion is no longer supported I found the following alternative:由于不再支持显式 byte1 到 uint 的转换,我找到了以下替代方法:
function toUint256(bytes memory _bytes, uint256 _start) internal pure returns (uint256) {
require(_bytes.length >= (_start + 32), "Read out of bounds");
uint256 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x20), _start))
}
return tempUint;
}
The bytes is the input in the ApproveAndCall function of the ERC20 Token字节是 ERC20 Token 的 ApproveAndCall function 中的输入
function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data);
return true;
}
which is sent over to the receiveApproval of my smart contract.它被发送到我的智能合约的receiveApproval。
function receiveApproval(address _from, uint _token, address _tokenContract, bytes memory _data) public {
if(!ERC20Interface(_tokenContract).transferFrom(_from, address(this), _token)) {
revert();
}
_0xChangeLib.place_sell_order(exchange, _from, _tokenContract, _token, _0xChangeLib.toUint256(_data, 0));
}
Could someone explain how this new BytesToUint256 works?有人能解释一下这个新的 BytesToUint256 是如何工作的吗? I can't get my head around the assembly code and how to use this function.
我无法理解汇编代码以及如何使用这个 function。 I don't understand the uint256 _start argument.
我不明白 uint256 _start 参数。 I am also not sure if I could use the same format as input as I was using.
我也不确定是否可以使用与输入相同的格式。 As argument I was converting a wei amount as a bytes, eg 100 wei = 0x100, with a simple function in javascript and sent over to the token address with Web3.js.
作为参数,我将 wei 数量转换为字节,例如 100 wei = 0x100,在 javascript 中使用简单的 function 并使用 Web3.js 发送到令牌地址。
I would like within ReceiveApproval function of the smart contract to call the BytesToUint function to further process the data.我想在智能合约的 ReceiveApproval function 中调用 BytesToUint function 来进一步处理数据。
Thank you very much in advance for your help!非常感谢您的帮助!
The _start
basically points to the byte index in bytes
array where the integer value starts. _start
基本上指向bytes
数组中 integer 值开始的字节索引。 The first 32 (or 0x20 in hex) bytes contain the length of the bytes
array and then starts the integer value which is stored in the next 32 bytes.前 32 个(或十六进制的 0x20)字节包含
bytes
数组的长度,然后开始存储在接下来的 32 个字节中的 integer 值。 The _start value is zero because second set of 32 bytes contain the integer value you need. _start 值为零,因为第二组 32 个字节包含您需要的 integer 值。 You can essentially convert this function to this.
您基本上可以将此 function 转换为此。
function toUint256(bytes memory _bytes)
internal
pure
returns (uint256 value) {
assembly {
value := mload(add(_bytes, 0x20))
}
}
With regards to your comment 0x0
would mean bytes array has length 1 ie the second zero and the require
statement expects the length of at least 32.关于您的评论,
0x0
表示字节数组的长度为 1,即第二个零,并且require
语句预计长度至少为 32。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.