# Netkiller Blockchain 手札

## 本文作者最近在找工作，有意向致电 13113668890

### Mr. Neo Chan, 陈景峯(BG7NYT)

http://www.netkiller.cnhttp://netkiller.github.iohttp://netkiller.sourceforge.net

http://www.netkiller.cn

http://netkiller.github.io

http://netkiller.sourceforge.net

QQ：13721218 请注明“读者”

QQ群：128659835 请注明“读者”

http://www.netkiller.cn

http://netkiller.github.io

http://netkiller.sourceforge.net

QQ：13721218 请注明“读者”

QQ群：128659835 请注明“读者”

## 4.5. 数据类型

### 4.5.1. 数值型

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

#### 4.5.1.1. 加 +，减 -，乘 *，除 ／ 运算演示

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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 <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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 <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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 <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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 <netkiller@msn.com>
//Home: http://www.netkiller.cn

contract 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 <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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. 获取字符串长度

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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;
}

}

### 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 <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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 <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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 <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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 <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract ArrayLength {

uint [] array = [1,2,3,4,5];

function getLength() public constant returns (uint) {

return array.length;
}

}

#### 4.5.5.2. push() 方法

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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 <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract Students {

struct Person {
string name;
uint age;
uint class;

}

Person person = Person("Neo",18,1);

function getPerson() public view returns(string){
return person.name;
}
}

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn

function getBalance(address _addr) public constant returns (uint){
}

}

### 4.5.9. mapping

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

pragma solidity ^0.4.20;
//Author: netkiller <netkiller@msn.com>
//Home: http://www.netkiller.cn
contract 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];
}
}

283 篇文章87 人订阅

0 条评论

## 相关文章

3687

1182

1202

3445

### Coderbyte-Challenger之Letter Capitalize（单词字母大写）

Have the function LetterCapitalize(str) take the str parameter being passed and...

893

### Java 8的新特性—终极版

1. 简介 毫无疑问，Java 8是Java自Java 5（发布于2004年）之后的最重要的版本。这个版本包含语言、编译器、库、工具和JVM等方面的十多个新特...

4226

8941

### Ruby学习笔记

Ruby语言中，以对象为基本单位，可以说所有的元素都是对象。按照之前对于面向对象程序的理解，对象是指包含了特定属性和方法集合的一组程序。对象由类来定义，具体的表...

832

25210

36311