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);
    }
    
}
### Solidity 编程语言基础教程 Solidity 是一种用于编写智能合约的面向对象高级编程语言,主要用于 Ethereum 平台上的开发工作[^1]。 #### 数据类型 Solidity 支持多种数据类型,包括布尔型 `bool`、整数类型 `int` 和无符号整数类型 `uint`、地址类型 `address` 以及字符串 `string`。每种类型的变量都有特定的作用范围和操作方法。 #### 函数定义 函数是实现业务逻辑的关键部分,在 Solidity 中可以通过如下方式声明一个简单的返回两个数值之和的函数: ```solidity function add(uint a, uint b) public pure returns (uint sum){ sum = a + b; } ``` 此代码片段展示了如何创建一个名为 `add` 的公共纯函数,它接受两个参数并返回它们相加的结果。 #### 合约结构体 合约可以被视作由状态变量、事件、函数组成的实体。下面是一个基本的合约例子: ```solidity pragma solidity ^0.8.0; contract SimpleStorage { // 定义存储的数据 uint storedData; function set(uint x) public { storedData = x; } function get() public view returns (uint) { return storedData; } } ``` 这段程序展示了一个简单储存功能的小合约,其中包含了设置 (`set`) 和获取 (`get`) 存储值的方法。 #### 部署与交互 要部署上述合约到区块链上,则需借助像 Remix IDE 这样的工具来编译源码,并通过 MetaMask 或其他钱包服务完成交易过程;之后就可以调用其公开接口来进行读写操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值