温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

solidity智能合约[9]-字节数组与运算

发布时间:2020-06-15 02:34:47 阅读:378 作者:jonson_jackson 栏目:开发技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

byte类型

有byte bytes1 bytes2 … bytes32
特殊的有byte == bytes1

后面的数字代表占了多少字节。1个字节在内存中占了8位

性质

固定字节数组不能修改长度和内容

字节一般用16进制来存储

16进制中的1个数字代表占了4位。

1234567
bytes1 public num1 = 0x6a;        //转换为10进制:106bytes2 public num2 = 0x6a6f;     //转换为10进制:27247bytes6 public num3 = 0x6a6f6e736f6e; bytes1 public a = 0x6a;//转换为2进制:0110   1010      bytes1 public b = 0x6f;//转换为2进制:0110   1111

字节可以有长度属性

123
function getlength() view public returns(uint,uint,uint){    return (num1.length,num2.length,num3.length);}

字节可以比较大小

不同类型的字节也可以比较大小

1234567891011121314151617181920212223242526
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<b;}    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;}

字节可以进行位运算

12345678910111213141516171819202122232425262728
// 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;}

完整代码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
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<b;    }        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;    }}
  • 本文链接: https://dreamerjonson.com/2018/11/14/solidity-9/

  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!

solidity智能合约[9]-字节数组与运算

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×