如何在Solidity(带汇编的)中使用BytesToUint函数?
How to use BytesToUint function in Solidity (the one with assembly)?
我正在使用以下函数将字节转换为 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;
}
由于不再支持从 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;
}
bytes为ERC20 Token的ApproveAndCall函数中的输入
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;
}
它被发送到我的智能合约的 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));
}
有人可以解释这个新的 BytesToUint256 是如何工作的吗?我无法理解汇编代码以及如何使用此功能。我不明白 uint256 _start 的说法。我也不确定我是否可以使用与输入相同的格式。作为参数,我将 wei 量转换为字节,例如100 wei = 0x100,在 javascript 中有一个简单的功能,并通过 Web3.js.
发送到代币地址
我想在智能合约的 ReceiveApproval 函数中调用 BytesToUint 函数来进一步处理数据。
非常感谢您的帮助!
_start
基本上指向 bytes
数组中整数值开始的字节索引。前 32 个(或十六进制的 0x20)字节包含 bytes
数组的长度,然后开始存储在接下来的 32 个字节中的整数值。 _start 值为零,因为第二组 32 个字节包含您需要的整数值。您基本上可以将此函数转换为此函数。
function toUint256(bytes memory _bytes)
internal
pure
returns (uint256 value) {
assembly {
value := mload(add(_bytes, 0x20))
}
}
关于您的评论 0x0
表示字节数组的长度为 1,即第二个零,require
语句预计长度至少为 32。
我正在使用以下函数将字节转换为 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;
}
由于不再支持从 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;
}
bytes为ERC20 Token的ApproveAndCall函数中的输入
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;
}
它被发送到我的智能合约的 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));
}
有人可以解释这个新的 BytesToUint256 是如何工作的吗?我无法理解汇编代码以及如何使用此功能。我不明白 uint256 _start 的说法。我也不确定我是否可以使用与输入相同的格式。作为参数,我将 wei 量转换为字节,例如100 wei = 0x100,在 javascript 中有一个简单的功能,并通过 Web3.js.
发送到代币地址我想在智能合约的 ReceiveApproval 函数中调用 BytesToUint 函数来进一步处理数据。
非常感谢您的帮助!
_start
基本上指向 bytes
数组中整数值开始的字节索引。前 32 个(或十六进制的 0x20)字节包含 bytes
数组的长度,然后开始存储在接下来的 32 个字节中的整数值。 _start 值为零,因为第二组 32 个字节包含您需要的整数值。您基本上可以将此函数转换为此函数。
function toUint256(bytes memory _bytes)
internal
pure
returns (uint256 value) {
assembly {
value := mload(add(_bytes, 0x20))
}
}
关于您的评论 0x0
表示字节数组的长度为 1,即第二个零,require
语句预计长度至少为 32。