固定字节数组转动态字节数组

要将固定长度的字节数组转换为动态长度的字节数组,需要首先创建动态数组,并挨个赋值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

pragma solidity ^0.4.23;

contract fixTodynamic{
bytes6 name = 0x6a6f6e736f6e;

function Todynamic() view public returns(bytes){
//return bytes(name);
bytes memory newName = new bytes(name.length);

//for循环挨个赋值
for(uint i = 0;i<name.length;i++){
newName[i] = name[i];
}
return newName;
}
}

本文链接:https://dreamerjonson.com/2018/11/19/solidity-13-fixtodynamic/

版权声明:本博客所有文章除特别声明外,均采用CC BY 4.0 CN协议许可协议。转载请注明出处!