以 solidity 方式访问内存字节的元素
Access elements of memory bytes in solidity
我在 solidity 中使用字节时遇到问题。
我的代码:
function get() public{
string memory sl = "asddsa";
bytes memory aa = bytes(sl);
log("123");
aa[0] = 2;
}
如果我 运行 此代码,我将不会收到任何日志。否则,如果我删除行 "aa[0] = 2",我将收到日志。
我不确定问题出在哪里,但似乎访问字节元素时出现问题。
以前有人遇到过这种问题吗?请帮我解决。谢谢。
顺便说一句,当我将该代码放入库中时,它 运行 没问题。
它没有记录,因为您的事务失败了。函数的最后一行 (aa[0] = 2;
) 试图将值设置为内存中的动态数组,这是不允许的。您需要将 aa
更改为存储变量,或者像这样声明数组的大小:
function get() public{
string memory sl = "asddsa";
byte[] memory aa = new byte[](bytes(sl).length);
log("123");
aa[0] = 2;
}
我在 solidity 中使用字节时遇到问题。 我的代码:
function get() public{
string memory sl = "asddsa";
bytes memory aa = bytes(sl);
log("123");
aa[0] = 2;
}
如果我 运行 此代码,我将不会收到任何日志。否则,如果我删除行 "aa[0] = 2",我将收到日志。 我不确定问题出在哪里,但似乎访问字节元素时出现问题。 以前有人遇到过这种问题吗?请帮我解决。谢谢。 顺便说一句,当我将该代码放入库中时,它 运行 没问题。
它没有记录,因为您的事务失败了。函数的最后一行 (aa[0] = 2;
) 试图将值设置为内存中的动态数组,这是不允许的。您需要将 aa
更改为存储变量,或者像这样声明数组的大小:
function get() public{
string memory sl = "asddsa";
byte[] memory aa = new byte[](bytes(sl).length);
log("123");
aa[0] = 2;
}