1. 程式人生 > >ethereum(以太坊)(十)--字節數組

ethereum(以太坊)(十)--字節數組

2個 UNC dex string allow convert cti div implicit

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(以太坊)(十)--字節數組