Solidity 数组类型。定长数组(bytes1)、不定长数组(bytes)与string之间的转换
demo.sol(定长数组,bytes1 … bytes32):
pragma solidity ^0.4.20;
contract fixedArray {
//bytes1 ... bytes32
bytes2 b2 = "xy"; // bytes2 长度必须是2个字符。 定义之后不可以修改。
bytes3 public b3 = "xy"; // bytes3 长度必须是3个字符(后面默认用0补齐)
uint public len = b3.length; // 长度属性只读(不能修改)
bytes8 b8 = "12345678";
bytes1 public b8_1 = b8[0]; // 支持下标。(不能通过下标修改元素)
// b2 = "aa" // 定义之后不可以修改。
}
demo.sol(数组,类型[]):
pragma solidity ^0.4.5;
contract array1Test{
uint len1 = 0 ;
function func() public {
uint[] memory value = new uint[](10); // 定长数组(元素uint类型)。 (memory修饰的局部变量)
value[0] = 1; // 支持下标(可以修改元素值)
//value.push(1); // 不可以添加新元素(长度不能变)
len1 = value.length; // 长度属性
//value.length = 20; // 长度不能修改
}
uint[] public arrayValue; // 不定长数组(元素uint类型)。状态变量(成员变量,默认storage修饰)
uint public len2;
function func2() public {
arrayValue = new uint[](10);
arrayValue[0] = 99; // 支持下标(可以修改元素值)
arrayValue.push(100); // 可以添加新元素(长度可以变)。 也支持pop方法
arrayValue.length = 50; // 长度可以变
len2 = arrayValue.length;
}
uint[7] value = [1,2,3,4,5,6,7]; // 定长数组。
function func3() public {
value[0] = 10; // 支持下标(可以修改元素值)
// value.length = 10; // 长度不可以修改。 不支持push、pop。
}
}
demo.sol(不定长数组,bytes (引用类型)):
pragma solidity ^0.4.4;
contract Test {
bytes public _name = new bytes(1); // 不定长数组。初始长度1
bytes public _name2;
function setLength(uint length) public {
_name.length = length; // 可以修改长度
}
function getLength() constant public returns (uint) {
return _name.length;
}
function setName(bytes name) public {
_name = name; // 数组的值可以修改
}
function changeName(bytes1 name) public {
_name[0] = name; // 可以通过下标修改元素
}
function setInside() public {
_name = "helloWorld";
_name2 = "HELLOWORLD2"; // 可以直接赋值
}
}
demo.sol(不定长数组,string—>bytes (引用类型)):
pragma solidity ^0.4.4;
contract Test {
string public _name = "lily";
function nameBytes() constant public returns (bytes) {
return bytes(_name); // 类型转换 string-->bytes(不定长数组)
}
function nameLength() constant public returns (uint) {
//uint len = _name.length; // string没有length属性
return bytes(_name).length;
}
function changeName() public {
bytes(_name)[0] = "L"; // 会直接作用于(修改)_name字符串
//_name[0] = "H"; //ERROR,不支持下标索引
}
function changeLength() public {
bytes(_name).length = 15; // bytes(不定长数组)
bytes(_name)[14] = "x"; // 会直接作用于(修改)_name字符串
// bytes(_name)[19] = "x"; // ERROR,不能超出下标范围
}
}
demo.sol(定长数组(bytes1)、不定长数组(bytes)、string之间的转换):
pragma solidity ^0.4.20;
contract Test {
bytes10 b10 = 0x68656c6c6f776f726c64; //helloworld 定长数组
//1. 定长字节数组-->变长字节数组 (拷贝)
//bytes bs10 = b10; // ERROR
bytes public bs10 = new bytes(b10.length);
function fixedBytesToBytes() public{
// (拷贝)
for (uint i = 0; i< b10.length; i++){
bs10[i] = b10[i];
}
}
//2. string-->变长字节数组
string greeting = "helloworld";
bytes public b1;
function StringToBytes() public {
b1 = bytes(greeting);
}
//3.变长字节数组-->string
string public str3;
function BytesToString() public {
str3 = string(bs10);
}
//4. 定长字节数组-->string
function FiexBytesToString() public {
//string tmp = string(b10); // ERROR。不可以直接转换,可以通过1.和3.间接转换
}
}
还没有评论,来说两句吧...