我所包含的代码片段将一个ASCII字符串值转换为一个uint
,只要该字符串本身是一个有效的无符号整数,并且在代码的屏幕截图中给出的在线“断言(_bytesValue我) >= 48 & uint8(_bytesValue我) <= 57”和在线"_ret += (uint8(_bytesValue我) -48)*j“上都有错误。
function parseInt(string memory _value)
public
returns (uint _ret) {
bytes memory _bytesValue = bytes(_value);
uint j = 1;
for(uint i = _bytesValue.length-1; i >= 0 && i < _bytesValue.length; i--)
{
assert(uint8(_bytesValue[i]) >= 48 && uint8(_bytesValue[i]) <= 57);
_ret += (uint8(_bytesValue[i]) - 48)*j;
j*=10;
}
}
发布于 2019-03-09 09:53:53
您可以将bytes1
转换为uint8
,然后进行比较。例如:
bytes memory _bytesValue = bytes('yo yo');
assert(uint8(_bytesValue[2]) >= 4);
uint _ret = uint8(_bytesValue[2]) - 4;
发布于 2019-03-09 00:50:02
从bytes
读取一个元素时,您将得到一个bytes1
。
您可能应该将您的bytes1
转换为uint8
https://ethereum.stackexchange.com/questions/68118
复制