【转载】solidity智能合约[13]-固定字节数组转动态字节数组


#1

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

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

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/>