Solidity 教程

Solidity

pragma solidity ^0.4.0;

contract demo {
    string name = "chj";

    function getName() view public returns(string) {
        return name;
    }

    function setName(string _name) public {
        name = _name;
    }

    // 相当于仅是一个普通函数,不会改变值,单节约 gas
    function pureTest(string _name) pure public returns(string) {
        return _name;
    }

    // 事实上它支持 c 语言一样的逻辑判断符
    function judge() public returns(bool) {
        bool a = false;
        return a;
    }
}

不加 view 这些玩意会被系统认为调用外部资源

pragma solidity ^0.4.0;

contract demo {
    // int 与 uint 都是 256 位
    int a = 12;
    uint b = 12;
    uint8 c = 12;

    function add(uint a, uint b) pure public returns(uint) {
        return a + b;
    }

    // 溢出的现象其实和 C 语言里的一样
    function flow() view public returns(uint) {
        return 256;
    }
}

bytes 是固定长度字节数组

bytes1 是八位,其他类推,通常用 16 进制表示

固定长度数组

pragma solidity ^0.4.0;

contract demo {
    // 注意:固定长度数组是不能修改的,类似于 Python 的元祖

    // 0111 1010
    bytes1 num1 = 0x7a;
    // 0111 1010 0110 1000
    bytes2 num2 = 0x7a68;

    // 1
    function getLength1() returns(uint) {
        return num1.length;
    }

    // 2
    function getLength2() returns(uint) {
        return num2.length;
    }
}

动态长度数组

pragma solidity ^0.4.0;

contract demo {
    bytes public name = new bytes(2);

    function InitName() {
        name[0] = 0x7a;
        name[1] = 0x68;
    }

    function getLength() view returns(uint) {
        return name.length;
    }

    function changeName() {
        name[0] = 0x88;
    }

    function changeLength() {
        name.length = 5;
    }

    // 动态添加数据
    function pushTest() {
        name.push(0x99);
    }
}

单引号和双引号没有区别

string 是特殊动态数组,utf8 类型

string 没有 Length 这个属性,因此不能获取它的长度

通过下标只能获取 Bytes 数据,不能将它强制转换为 string !!!

string 支持中文,但是注意,它是三个字节的

pragma solidity ^0.4.0;

contract demo {
    string name = "zhangsan";

    function getLength() view returns(uint) {
        // 只能通过这种方式获取长度
        return bytes(name).length;
    }

    function getByte() returns(bytes1) {
        // 只能通过这种方式获取指定下标的数据
        return bytes(name)[0];
    }

    function getName() returns(bytes) {
        return bytes(name);
    }

    function setByte() {
        bytes(name)[0] = 'L';
    }
}s

固定长度字节数组的转换

pragma solidity ^0.4.0;

contract demo {
    bytes3 name = 0x7a6865;

    // 0x7a
    function getBytes1() returns(bytes1) {
        return bytes1(name);
    }

    // 0x7a68
    function getBytes2() returns(bytes2) {
        return bytes2(name);
    }

    // 0x7a6865
    function getBytes3() returns(bytes3) {
        return bytes3(name);
    }
}

固定长度数组转动态长度数组

pragma solidity ^0.4.0;

contract demo {
    bytes3 name = 0x7a6865;

    function toDynamic() view returns(bytes) {
        // 之前没有 memory 是因为声明在函数体外部
        bytes memory newName = new bytes(name.length);

        // 索引必须是 uint
        for (uint i = 0; i < name.length; i++) {
            newName[i] = name[i];
        }

        return newName;
    }
}

bytes 转 string(动态长度数组转化为 string)

pragma solidity ^0.4.0;

contract demo {
    bytes name = new bytes(2);

    function Init() {
        name[0] = 0x7a;
        name[1] = 0x68;
    }

    function toStrin
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Solidity是一种用于编写智能合约的编程语言。它是专门为以太坊平台设计的,用于创建去中心化应用程序(DApps)。Solidity支持多种特性,包括枚举和结构体。 枚举(enum)是一种可用来创建由一定数量的常量值构成的自定义类型。在Solidity中,枚举类型可以用来定义一组相关的状态或选项。例如,你可以使用枚举来定义一个投票合约中的不同状态,如"Created"、"Locked"和"InValid"。\[1\] 结构体(struct)是一种可以将多个变量分组的自定义类型。在Solidity中,结构体可以用来定义一个包含多个属性的数据结构。例如,在一个投票合约中,你可以使用结构体来定义一个投票人的属性,如权重、是否已投票、委托地址和投票选项。\[3\] 通过使用Solidity的枚举和结构体,你可以更好地组织和管理智能合约中的数据和状态。这些特性使得Solidity成为开发去中心化应用程序的强大工具。如果你想学习更多关于Solidity的内容,可以查阅Solidity的官方文档和教程。 #### 引用[.reference_title] - *1* *2* *3* [Solidity入门(1)](https://blog.csdn.net/weixin_49489840/article/details/124184205)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值