solidity_入门1

solidity学习1

入门操作
pragma solidity ^0.4.16;

contract HelloWorld {
    string myName = "xujialu,helloworld";
    
    
    
    bytes1  num1 = 0x12;  
    
    bytes2  num2 = 0x1212; 
    
    bytes3 public num3=0x123456;
    
    
    bool flag = false;
    
    
    
    
    
    string testword = "xujialu"; //0x78756a69616c75
    string testword1='asldhlkasdh';
    string testword2='^*^*%*()*-/';
    string testword3='中文测试';
    string testword4='中文测试2222';
    
    function stringLength() public view returns(uint){
        // return testword.length 直接返回string类型的字符串legth会报错,因为string没有这个方法
        // length这个方法bytes中有
        return bytes(testword).length;
    }function stringjinzhi() public view returns(bytes)
    {
        return bytes(testword);   //stringchange执行后十六进制0x41756a69616c75;
    }
    
    function stringchange() public {
        // testword[0]="A";这样会直接报错,string类型的不能直接改变
        //借助bytes
        bytes(testword)[0]="A";
    }
    
    function stringchangtest() public view returns(bytes1){
        return bytes(testword)[0];  // 原来 0x78756a69616c75 现在 0x41756a69616c75  0x41
    }
    
    //string 类型是怎么存储字符串的,对于字母和一般特殊字符,都是一个字符占一个字节
    //对于中文字符是一个字符占3个字节
    
    function stringjinzhitest1() public view returns(uint,bytes){
        return (bytes(testword1).length,bytes(testword1));
    }
    
    function stringjinzhitest2() public view returns(uint,bytes){
        return (bytes(testword2).length,bytes(testword2));
    }
    function stringjinzhitest3() public view returns(uint,bytes){
        return (bytes(testword3).length,bytes(testword3));
    }
    
    function stringjinzhitest4() public view returns(uint,bytes){
        return (bytes(testword4).length,bytes(testword4));
    }
    
    //固定长度字节数组bytes的截断
    //结论:位数足够则保留前面的,位数不够再后面加0
    bytes10 testword10=0x68656c6c6f776f726c64; //helloworld
    function transbytes1() public view returns(bytes1){
        return bytes1(testword10);  // 0x68
    }
    function transbytes2() public view returns(bytes5){
        return bytes5(testword10);  //  0x68656c6c6f
    }
    function transbytes3() public view returns(bytes12){
        return bytes12(testword10);  //0x68656c6c6f776f726c640000
    }   
    
    
    
    
    
    
    // view pure
    function getName()  public view returns(string){
        return myName;
    }
    
    // + - * / yunsuan
    function chengTest(uint a,uint b) pure public returns(uint){
        return a*b;
    } 
    
    function addTest(uint a,uint b) pure public returns(uint){
        return a+b;
    }
    function jianTest(uint a,uint b) pure public returns(uint){
        return a-b;
    }
    
    // bool && || 
    function boolTest (bool a) view public returns (bool){
        return flag && a;
    }
    function boolTest1 (bool a) view public returns (bool){
        return flag || a;
    }
    function boolTest2 (bool a) view public returns (bool){
        return flag == a;
    }
    
    // ** % 
    function quyu (uint a,uint b) pure public returns (uint){
        return a%b;
    }
    
    function miTest (uint a,uint b) pure public returns (uint){
        return a**b;
    }
    
    //weiyunsuan
     function Wyutest(uint8 a,uint8 b)  public pure returns(uint8){
        return a&b;
    }
    function Whuotest(uint8 a,uint8 b)  public pure returns(uint8){
        return a|b;
    }
    function Wfantest(uint8 a)  public pure returns(uint8){
        return ~a;
    }
    function Wyihuotest(uint8 a,uint8 b)  public pure returns(uint8){
        return a^b;
    }
    function zuoyitest(uint8 a,uint8 b)  public pure returns(uint8){
        return a<<b;
    }
    function youyitest(uint8 a,uint8 b)  public pure returns(uint8){
        return a>>b;
    }
    function getLength() public view returns (uint8,uint16){
        
        return (num1.length,num2.length);
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值