区块链基础入门-数组

1.固定长度字节数组

pragma solidity ^0.4.0;
contract ByteArray{
   bytes1 public num1=0x7a;
   bytes2 public num2=0x7a68;
   bytes12 public num3=0x7a68656e67a69616e787576e;
   
   function getLength()returns(uint){
       return num1.length;
   }
   function getLength2()returns(uint){
       return num2.length;
   }
   function getLength3()returns(uint){
       return num3.length;
   }
}

2.动态字节数组

pragma solidity ^0.4.0;
contract DynamicByte{
    bytes public name=new bytes(2);
    function InitName(){
        name[0]=0x7a;
        name[1]=0x68;
    }
    function getLength()view returns(uint)
    {
        return name.length;
    }
    function changeName()
    {
        name[0]=0x88;
    }
     function changeLength(){
         name.length=5;
     }
}

3.string型特殊动态数组

pragma solidity ^0.4.0;
contract DynamicString{
    string name="abc";
    function getLength()view returns(uint){
        return bytes(name).length;
    }
    function changeName()returns(bytes1){
       // return name[0];
       return bytes(name)[0];
    }
    function getName()view returns(bytes){
        return bytes(name);
    }
     function changeName2()returns(bytes1){
       // return name[0];
       return bytes(name)[0]='l';
    }
}

特殊字符数组

pragma solidity ^0.4.0;
contract DynamicString{
    string name="abc";
    string name2="&*$#@";
    string name3="上海";
    function getLength()view returns(uint){
        return bytes(name).length;
    }
     function getLength2()view returns(uint){
        return bytes(name2).length;
    }
    function getLength3()view returns(uint){
        return bytes(name3).length;
    }
    function changeName()returns(bytes1){
       // return name[0];
       return bytes(name)[0];
    }
    function getChineseName()view returns(bytes){
        return bytes(name3);//0xe4b88ae6b5b7
    }
     function changeName2()returns(bytes1){
       // return name[0];
       return bytes(name)[0]='l';
    }
}

4.固定长度字节数组的转换

pragma solidity ^0.4.0;
contract changeFisBytes{
         bytes12 name=0x7a68656e67a69616e787576e;
         function changeBytes1() view returns(bytes1){
             return bytes1(name);
         }
         function changeBytes2() view returns(bytes2){
             return bytes2(name);
         }
         function changeBytes16() view returns(bytes16){
             return bytes16(name);
         }
}
pragma solidity ^0.4.0;
contract fixToDynamic{
         bytes12 name=0x7a68656e67a69616e787576e;
         function fixBytesToDynamicBytes()view returns (bytes){
             bytes memory  newName=new bytes(name.length);
             for(uint i=0;i<name.length;i++){
                 newName[i]=name[i];
             }
             return newName;
         }
         
}

5.bytes与string的相互转换
bytes的初始化————new bytes 获取bytes的长度,内容,并修改长度内容
string————不能够直接获取长度和内容,需要转换成bytes 、特殊字符,特别是中文占了3个字节
固定长度字节数组之间的相互转换
固定长度字节数组————bytes可变长度字节数组
将bytes转化为string

pragma solidity ^0.4.0;
contract BytesToString{
         
         bytes name=new bytes(2);
         function Init(){
             name[0]=0x7a;
             name[1]=0x68;
         }
         function bytesToString()view returns(string){
             return string(name);
         }
}

将固定的bytes 转化为string

pragma solidity ^0.4.0;
contract Bytes32ToString{
         
         bytes2 name=0x7a68;
         function changeIt()returns(string){
            // return string(name);
         }
         function bytes32ToString(bytes32 _newname)view returns(string){
             bytes memory  newname = new bytes(_newname.length);
             for(uint i=0;i<_newname.length;i++){
                 newname[i]=_newname[i];
             }
             return string(newname);
         }
}
pragma solidity ^0.4.4;
contract Bytes32ToString{
         
         bytes2 name=0x7a68;
         function changeIt()returns(string){
            // return string(name);
         }
         function bytes32ToString(bytes32 _newname)view returns(string){
            uint count=0;
            for(uint i=0;i<_newname.length;i++){
                bytes1 char=_newname[i];
                if(char !=0){
                    count++;
                }
            }
            bytes memory newname=new bytes(count);
            for(uint j=0;j<count;j++){
            
                
            newname[j]=_newname[j];
            }
            return string(newname);
         }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值