ethereum - 用于字节操作的 Solidity 代码无法使用带有 Solidity 0.8.0 的安全帽编译器进行编译
问题描述
我正在编译Sol 0.5.0
使用0.8.0
编译器编写的 OpenSea 项目的代码,但出现错误:
ParserError: Expected primary expression.
--> contracts/Strings.sol:53:25:
|
53 | bstr[k--] = byte(uint8(48 + _i % 10));
| ^^^^
Error HH600: Compilation failed
原始代码位于:https ://github.com/ProjectOpenSea/opensea-creatures/blob/master/contracts/Strings.sol ,它使用Sol 0.5.0
并可能是用松露编译的。我正在尝试使用Hardhat
and 0.8.0
。代码复制如下:
pragma solidity ^0.8.0;
library Strings {
// via https://github.com/oraclize/ethereum-api/blob/master/oraclizeAPI_0.5.sol
function strConcat(string memory _a, string memory _b, string memory _c, string memory _d, string memory _e) internal pure returns (string memory) {
bytes memory _ba = bytes(_a);
bytes memory _bb = bytes(_b);
bytes memory _bc = bytes(_c);
bytes memory _bd = bytes(_d);
bytes memory _be = bytes(_e);
string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length);
bytes memory babcde = bytes(abcde);
uint k = 0;
for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
for (uint i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
for (uint i = 0; i < _bc.length; i++) babcde[k++] = _bc[i];
for (uint i = 0; i < _bd.length; i++) babcde[k++] = _bd[i];
for (uint i = 0; i < _be.length; i++) babcde[k++] = _be[i];
return string(babcde);
}
function strConcat(string memory _a, string memory _b, string memory _c, string memory _d) internal pure returns (string memory) {
return strConcat(_a, _b, _c, _d, "");
}
function strConcat(string memory _a, string memory _b, string memory _c) internal pure returns (string memory) {
return strConcat(_a, _b, _c, "", "");
}
function strConcat(string memory _a, string memory _b) internal pure returns (string memory) {
return strConcat(_a, _b, "", "", "");
}
function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len - 1;
while (_i != 0) {
bstr[k--] = byte(uint8(48 + _i % 10));
_i /= 10;
}
return string(bstr);
}
}
注意我改变pragma
了顶部。对我来说一切都很好,所以我不确定问题出在哪里,除了它在这条线上: bstr[k--] = byte(uint8(48 + _i % 10));
解决方案
使用bytes1
而不是byte
.
该类型
byte
已被删除。它是 的别名bytes1
。
来源:https ://docs.soliditylang.org/en/v0.8.3/080-break-changes.html#silent-changes-of-the-semantics
推荐阅读
- visual-studio - VS2015,没有断点使用的变量观察。可能吗?
- nginx - nginx - 有时提供以前版本的文件(缓存?)
- javascript - 如何用列表渲染对象
- c# - 当文件被另一个进程使用时复制文件
- python - 为什么批量标准化中的梯度检查不起作用?
- python-3.x - Python 3:在不同的行上打印多个变量
- node.js - 如何向 Stripe Charge 添加数量字段?JavaScript
- django - TypeError:“用户”对象不可下标
- node.js - 使用正则表达式从带有节点的 csv 文件中删除 \n 和 \r
- python-3.x - 在熊猫中选择最大值特殊条件?