首页 > 编程语言 > 详细

ethereum(以太坊)(十)--字节数组

时间:2018-11-16 18:17:04      阅读:294      评论:0      收藏:0      [点我收藏+]
pragma solidity ^0.4.0;

contract byte1{
    /*
    固定大小字节数组(Fixed-size byte arrays)
    固定大小字节数组可以通过bytes1,bytes2...bytes32声明,byte=byte1
    bytes1 只能存储1个字节,也就是二进制的8位内容   //一个字节=8位2进制/一个字母/符号/(1/3汉字)
    bytes2 只能存储2个字节,也就是二进制的8*2位内容
    bytes32 只能存储32个字节,也就是二进制的8*32=256位内容
    十六进制:1 2 3 4 5 6 7 8 9 a b c d e f
    */
    bytes1 a1=0x63; //0110 0011
    //byte   a2=0x632; Type int_const 1586 is not implicitly convertible to expected type bytes1

    bytes1 b01 = 0x6c; //0110 1100  10->16  12*16**0 +6*16**1 = 108
    bytes1 b11 = 0x69; //0110 1001  10->16  9*16**0 +6*16**1  = 105

    //比较操作符 <= , <,>=,>,==,!=

    function max() constant returns(bool){
        return b01 >= b11; //true
    }

    //位操作符 &,|,^,~,>>,<<
    function test1() constant returns(bytes1){
        //return b01 | b11;
        /*
        0110 1100
        0110 1001
        0110 1101 ->0x6d
        */
        return b01 ^ b11;
        /*
        0110 1100
        0110 1001
        0000 0101 -> 0x05
        */
    }
    //0x03 13 23 33 43 53 63 73 83
    //  0   1  2  3  4  5  6  7  8
    bytes9 b9 =0x031323334353637383;
    function testindex() constant returns(uint){
        //return b9[4]; //0x43
        return b9.length; //9  .length onlyread
    }
    //不可变数组 (长度不可变:b.length|内部存储的数据不可变:bytes[0])
    /*
    function setlen(uint8 a){ //TypeError: Expression has to be an lvalue
        b9.length = a;
    }

    function setvalue(byte a){ //TypeError: Expression has to be an lvalue
        b9[0] = a;
    }
    */

    bytes9 public g = 0x6c697975656368756e; //liyuechun

    string public name =‘liyuechun‘;//动态字符串数组

    function gByteLength() constant returns(uint){
        return g.length;
    }
    function stingToBytes() constant returns(bytes){
        return bytes(name);//还是string类型的动态字节数组
    }
    function setgByteLength(uint alen){
        bytes(name).length = alen;
    }

    function setgByteValue(bytes1 z){
        bytes(name)[0]= z;
    }
}

pragma solidity ^0.4.0;

contract byte1{
    bytes public b = new bytes(1); //bytes  声明动态大小字节数组
    //bytes(1)=0x00 bytes(2)=0x0000  定义动态字节数组长度
    //bytes public b = bytes(1); TypeError: Explicit type conversion not allowed from "int_const 1" to "bytes storage pointer"

    function setLeng(uint a) public{ //3 0x000000 |0 0x
        b.length = a;
    }

    function setValus(byte d,uint c ) public{ //0x06,0
        b[c] = d;
    }

    function setclear() public{
        delete b;
    }
}

 

ethereum(以太坊)(十)--字节数组

原文:https://www.cnblogs.com/eilinge/p/9970747.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!