千家信息网

Solidity数据类型有哪些

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容介绍了"Solidity数据类型有哪些"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!4.5.
千家信息网最后更新 2026年11月01日Solidity数据类型有哪些

本篇内容介绍了"Solidity数据类型有哪些"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

4.5. 数据类型

4.5.1. 数值型

int/uint:变长的有符号或无符号整型。变量支持的步长以8递增,支持从uint8到uint256,以及int8到int256。需要注意的是,uint和int默认代表的是uint256和int256。

有符号整型能够表示负数的代价是其能够存储正数的范围的缩小,因为其约一半的数值范围要用来表示负数。如:uint8的存储范围为0 ~ 255,而int8的范围为-127 ~ 127

支持的运算符:



比较:<=,<,==,!=,>=,>,返回值为bool类型。

位运算符:&,|,(^异或),(~非)。

数学运算:+,-,一元运算+,*,/,(%求余),(**次方),(<<左移),(>>右移)。

小数由"."组成,在他的左边或右边至少要包含一个数字。如"1.",".1","1.3"均是有效的小数。

4.5.1.1. 加 +,减 -,乘 *,除 / 运算演示
pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract Math {  function mul(int a, int b) public pure returns (int) {      int c = a * b;      return c;  }  function div(int a, int b) public pure  returns (int) {      int c = a / b;      return c;  }  function sub(int a, int b) public pure  returns (int) {            return a - b;  }  function add(int a, int b) public pure  returns (int) {      int c = a + b;      return c;  }}
4.5.1.2. 求余 %
pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract Math {  function m(int a, int b) public pure returns (int) {      int c = a % b;      return c;  }}
4.5.1.3. 幂运算
pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract Math {  function m(uint a, uint b) public pure returns (uint) {      uint c = a**b;      return c;  }}
4.5.1.4. 与 &,| 或,非 ~,^ 异或
pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract Math {  function yu() public pure returns (uint) {      uint a = 3; // 0b0011      uint b = 4; // 0b0100          uint c = a & b; // 0b0000      return c; // 0  }  function huo() public pure returns (uint) {      uint a = 3; // 0b0011      uint b = 4; // 0b0100          uint c = a | b; // 0b0111      return c; // 7  }  function fei() public pure returns (uint8) {      uint8 a = 3; // 0b00000011      uint8 c = ~a; // 0b11111100      return c; // 0  }    function yihuo() public pure returns (uint) {      uint a = 3; // 0b0011      uint b = 4; // 0b0100          uint c = a ^ b; // 0b0111      return c; // 252  }}
4.5.1.5. 位移
pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract Math {  function leftShift() public pure returns (uint8) {      uint8 a = 8; // 0b00001000      uint8 c = a << 2; // 0b00100000      return c; // 32  }  function rightShift() public pure returns (uint8) {      uint8 a = 8; // 0b00001000      uint8 c = a >> 2; // 0b00000010      return c; // 2  }}



a << n 表示a的二进制位向左移动n位,在保证位数没有溢出的情况下等价于 a乘以2的n次方。
a >> n 表示a的二进制位向右移动n位,在保证位数没有溢出的情况下等价于 a除以2的n次方。

4.5.2. 字符串

string 字符串类型,字符串可以通过""或者''来定义字符串的值

pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract StringTest {    string name;        function StringTest() public{        name = "default";    }    function setName(string _name) public{        name = _name;    }    function getName() public view returns(string){        return name;    }}
4.5.2.1. 获取字符串长度

在 Solidity 中想获得字符串长度必须转成 bytes 类型然后使用 length 属性获得。bytes(string).length

pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract StringTest {            string public name = "http://www.netkiller.cn";        function nameBytes() public constant returns (bytes) {                return bytes(name);    }        function nameLength() public constant returns (uint) {                return bytes(name).length;    }    function length(string _name) public pure returns (uint) {                return bytes(_name).length;    }    }

提示

注意:汉字采用UTF8编码,一个汉字等于3个字节,当你使用 length("景峯") 测试时会返回长度 6。


4.5.3. 布尔(Booleans)

bool: 可能的取值为常量值true和false。支持的运算符:

! 逻辑非&& 逻辑与|| 逻辑或== 等于!= 不等于bool a = true;bool b = !a;// a == b -> false// a != b -> true// a || b -> true// a && b -> false

4.5.4. 字节类型

bytes names = "netkiller"bytes9 _names = "netkiller";bytes(name)[0] = 0xFF;bytes memory _tmp = new bytes(3);_tmp[0] = 0x4e;_tmp[1] = 0x65;_tmp[2] = 0x6f;
pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract BytesTest {        bytes names = "netkiller";        function get() public view returns (bytes) {                return names;    }    function getBytes2() public pure returns (bytes2) {        bytes9 _names = "netkiller";        return bytes2(_names);    }    function bytesToString() public constant returns (string) {                return string(names);    }       function copyBytes(bytes b) public pure returns (bytes) {              bytes memory tmp = new bytes(b.length);              for(uint i = 0; i < b.length; i++) {                      tmp[i] = b[i];       }              return tmp;    }        function bytesToString2() public pure returns (string) {        bytes memory _tmp = new bytes(3);        _tmp[0] = 0x4e;        _tmp[1] = 0x65;        _tmp[2] = 0x6f;        return string(_tmp);    }   }

.length可以动态修改字节数组的长度

pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract BytesTest2 {        // 初始化一个两个字节空间的字节数组    bytes public array = new bytes(2);        // 设置修改字节数组的长度    function setLength(uint _len) public {        array.length = _len;    }        // 返回字节数组的长度    function getLength() constant public returns (uint) {        return array.length;    }        // 往字节数组中添加字节    function pushArray(byte _tmp) public{        array.push(_tmp);    }    }

4.5.5. 数组

//创建一个memory的数组        uint[] memory a = new uint[](7);                uint[] x = [uint(1), 3, 4];            bytes memory b = new bytes(10);

二维数组

uint [2][3] T = [[1,2],[3,4],[5,6]];
pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract ArrayTest {        uint [] array = [1,2,3,4,5];        // 通过for循环计算数组内部的值的总和    function sum() constant public returns (uint) {        uint num = 0;        for(uint i = 0; i < array.length; i++) {            num = num + array[i];        }        return num;    }        function sumNumbers(uint[] _numbers) public pure returns (uint) {        uint num = 0;        for(uint i = 0; i < _numbers.length; i++) {            num = num + _numbers[i];        }        return num;    }    }
4.5.5.1. length

.length 属性是活动数组的尺寸

pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract ArrayLength {        uint [] array = [1,2,3,4,5];        function getLength() public constant returns (uint) {                return array.length;    }    }
4.5.5.2. push() 方法

通过 push 可以向数组中添加数据

pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract ArrayLength {        uint [] array = [1,2,3,4,5];        function pushArray() public {                array.push(6);    }        function getLength() public constant returns (uint) {                return array.length;    }    }

4.5.6. 枚举类型

State 就是一个自定义的整型,当枚举数不够多时,它默认的类型为uint8,当枚举数足够多时,它会自动变成uint16,枚举下标定义从左至右从零开始。

New=0, Pending=1, Done=2, Deleted=3

访问枚举方式 State.New 实际等于数字 0

pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract EnumTest {    enum State { New, Pending, Done, Deleted }    State state = State.New;    function set(State _state) public {        state = _state;    }    function get() constant public returns (State) {        return state;    }}

枚举用来定义状态

pragma solidity ^0.4.0;contract Purchase {    enum State { Created, Locked, Inactive } // Enum}

4.5.7. 结构体

定义结构体

struct Voter {        uint weight; // weight is accumulated by delegation        bool voted;  // if true, that person already voted        address delegate; // person delegated to        uint vote;   // index of the voted proposal    }    // This is a type for a single proposal.    struct Proposal {        bytes32 name;   // short name (up to 32 bytes)        uint voteCount; // number of accumulated votes    }

演示例子

pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract Students {        struct Person {        string name;        uint age;        uint class;            }    Person person = Person("Neo",18,1);    function getPerson() public view returns(string){        return person.name;    }}

4.5.8. address

address public minter;

下面是一个获得账号余额的例子。

pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract AddressTest{        function getBalance(address _addr) public constant returns (uint){        return _addr.balance;    }}
4.5.8.1. payable
4.5.8.2. .value()
4.5.8.3. .gas()

4.5.9. mapping

mapping 就是图数据结构,由 key 和 value 组成。

pragma solidity ^0.4.20;//Author: netkiller //Home: http://www.netkiller.cncontract MappingExample {        mapping(uint => string) map;    function put(uint key, string value) public {        map[key] = value;    }        function get(uint key) constant public returns (string) {        return map[key];    }}

"Solidity数据类型有哪些"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

数组 类型 字节 运算 字符 字符串 长度 数据 范围 支持 情况 符号 结构 运算符 逻辑 二进制 位数 例子 内容 实际 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 中关村互联网科技集团 互联网巨头反垄断金融科技 互联网科技金融 北京互联网科技 华宇互联网科技 猪兼强互联网科技 老年人脱离互联网科技的作文 互联网科技博主是什么意思 汽车互联网创新科技园 宁夏众和广鑫互联网科技有限公司 语蝶互联网科技有限公司 苏州五指互联网络科技有限 互联网媒体平台算高科技产业 996高科技互联网公司 互联网科技股为什么大涨 南京一网一红互联网科技有限公司 西安有哪些互联网高科技企业 互联网领先科技活动 长沙易易互联网科技有限公司 如何强化互联网金融科技 手冲咖啡和咖啡机区别 互联网科技产业 互联网科技社 成都仁贤领冠互联网科技有限公司 金融科技与互联网金融相同和不同 移动互联网科技公司 烟台灵鸽互联网科技有限公司 上海乐五互联网科技有限公司 中国互联网公司的科技投入 互联网金融金融科技
0