简体   繁体   English

如何在 Solidity 中使用 BytesToUint function(带有装配的)?

[英]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.

相关问题 如何在汇编中使用负数作为一个? - how to use negative numbers as one in assembly? 在Assembly中使用rdtsc函数 - Use rdtsc function in Assembly 如何在装配中使用外部 C function? - How to use an external C function in assembly? 如何使用 Solidity 智能合约中的程序集写入任意存储槽? - How to write to an arbitrary storage slot using assembly in Solidity smart contracts? Solidity inline-assembly:在一个合约中调用其他函数并使用接口 - Solidity inline-assembly: Calling other functions within one contract and using interfaces ARM Assembly:如何在ARM Assembly函数内部传递和使用指针数组 - ARM Assembly: How to pass and make use of a array of pointers inside an ARM Assembly function 为什么我不能让 bool 在 Solidity Assembly function 中正常工作? - Why can't I get bool to work properly in Solidity Assembly function? 如何在Assembly中使用for循环? - How to use for loop in Assembly? 如何在装配中调用 function? - how to call a function in assembly? 如何使用其他程序集文件中的字节以及如何通过当前正在运行的程序集运行另一个程序集文件 - How to use Bytes from other assembly files and how to run another assembly file through the current running one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM