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

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

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;
   }
}

image.png