byte类型
有byte bytes1 bytes2 … bytes32
特殊的有byte == bytes1
为城东等地区用户提供了全套网页设计制作服务,及城东网站建设行业解决方案。主营业务为成都网站设计、
成都网站制作、城东网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
后面的数字代表占了多少字节。1个字节在内存中占了8位
性质
固定字节数组不能修改长度和内容
字节一般用16进制来存储
16进制中的1个数字代表占了4位。
| 12
 3
 4
 5
 6
 7
 
 | bytes1 public num1 = 0x6a;        //转换为10进制:106bytes2 public num2 = 0x6a6f;     //转换为10进制:27247
 
 bytes6 public num3 = 0x6a6f6e736f6e;
 
 bytes1 public a = 0x6a;//转换为2进制:0110   1010
 bytes1 public b = 0x6f;//转换为2进制:0110   1111
 
 | 
字节可以有长度属性
| 12
 3
 
 | function getlength() view public returns(uint,uint,uint){return (num1.length,num2.length,num3.length);
 }
 
 | 
字节可以比较大小
不同类型的字节也可以比较大小
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 
 | function  test1()  public view returns(bool){return  a>b;
 }
 
 function  test2() public view returns(bool){
 return  a>=b;
 }
 
 function  test3()  public  view returns(bool){
 return  a}
 function  test4() public   view returns(bool){
 return  a<=b;
 }
 
 function  test5() public view returns(bool){
 return  a==b;
 }
 
 function  test6() public view returns(bool){
 return  a!=b;
 }
 
 function  test7() public view returns(bool){
 return num2 >num1;
 }
 
 | 
字节可以进行位运算
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 
 | // 0110 1010// 0110 1111
 
 //&0110 1010   106    0x6a
 //|0110 1111   111    0x6f
 //^0000 0101   5      0x05
 //~1001 0101   149    0x95
 //<1101 0100   212    0xd4
 //>0011 0101   53     0x35
 function  weiTest1() public view returns(bytes1){
 return a & b;
 }
 function  weiTest2() public view returns(bytes1){
 return a | b;
 }
 function  weiTest3() public view returns(bytes1){
 return a ^ b;
 }
 function  weiTest4() public view returns(bytes1){
 return ~a;
 }
 
 function  weiTest5() public view returns(bytes1){
 return a<<1;
 }
 function  weiTest6() public view returns(bytes1){
 return a >>1;
 }
 
 | 
完整代码
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 
 | pragma solidity ^0.4.23;
 
 contract  bytesTest{
 //0x6a6f6e736f6e
 bytes1 public num1 = 0x6a;        //106
 bytes2 public num2 = 0x6a6f;     //27247
 
 bytes6 public num3 = 0x6a6f6e736f6e;
 
 bytes1 public a = 0x6a;//0110   1010      106
 bytes1 public b = 0x6f;//0110   1111      111
 
 
 
 function getlength() view public returns(uint,uint,uint){
 return (num1.length,num2.length,num3.length);
 }
 
 // function changeLength()  public {
 //     num1.length = 9;
 // }
 
 function  test1()  public view returns(bool){
 return  a>b;
 }
 
 function  test2() public view returns(bool){
 return  a>=b;
 }
 
 function  test3()  public  view returns(bool){
 return  a   }
 function  test4() public   view returns(bool){
 return  a<=b;
 }
 
 function  test5() public view returns(bool){
 return  a==b;
 }
 
 function  test6() public view returns(bool){
 return  a!=b;
 }
 
 function  test7() public view returns(bool){
 return num2 >num1;
 }
 
 
 // 0110 1010
 // 0110 1111
 
 //&0110 1010   106    0x6a
 //|0110 1111   111    0x6f
 //^0000 0101   5      0x05
 //~1001 0101   149    0x95
 //<1101 0100   212    0xd4
 //>0011 0101   53     0x35
 function  weiTest1() public view returns(bytes1){
 return a & b;
 }
 function  weiTest2() public view returns(bytes1){
 return a | b;
 }
 function  weiTest3() public view returns(bytes1){
 return a ^ b;
 }
 function  weiTest4() public view returns(bytes1){
 return ~a;
 }
 
 function  weiTest5() public view returns(bytes1){
 return a<<1;
 }
 function  weiTest6() public view returns(bytes1){
 return a >>1;
 }
 
 }
 
 | 
![solidity智能合约[9]-字节数组与运算](/upload/otherpic43/239049.jpg)
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
                                                
                                                当前题目:solidity智能合约[9]-字节数组与运算-创新互联                                                
                                                本文URL:
http://www.scyingshan.cn/article/coigic.html