1.固定长度字节数组
pragma solidity ^0.4.0;
contract ByteArray{
bytes1 public num1=0x7a;
bytes2 public num2=0x7a68;
bytes12 public num3=0x7a68656e67a69616e787576e;
function getLength()returns(uint){
return num1.length;
}
function getLength2()returns(uint){
return num2.length;
}
function getLength3()returns(uint){
return num3.length;
}
}
2.动态字节数组
pragma solidity ^0.4.0;
contract DynamicByte{
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;
}
}
3.string型特殊动态数组
pragma solidity ^0.4.0;
contract DynamicString{
string name="abc";
function getLength()view returns(uint){
return bytes(name).length;
}
function changeName()returns(bytes1){
// return name[0];
return bytes(name)[0];
}
function getName()view returns(bytes){
return bytes(name);
}
function changeName2()returns(bytes1){
// return name[0];
return bytes(name)[0]='l';
}
}
特殊字符数组
pragma solidity ^0.4.0;
contract DynamicString{
string name="abc";
string name2="&*$#@";
string name3="上海";
function getLength()view returns(uint){
return bytes(name).length;
}
function getLength2()view returns(uint){
return bytes(name2).length;
}
function getLength3()view returns(uint){
return bytes(name3).length;
}
function changeName()returns(bytes1){
// return name[0];
return bytes(name)[0];
}
function getChineseName()view returns(bytes){
return bytes(name3);//0xe4b88ae6b5b7
}
function changeName2()returns(bytes1){
// return name[0];
return bytes(name)[0]='l';
}
}
4.固定长度字节数组的转换
pragma solidity ^0.4.0;
contract changeFisBytes{
bytes12 name=0x7a68656e67a69616e787576e;
function changeBytes1() view returns(bytes1){
return bytes1(name);
}
function changeBytes2() view returns(bytes2){
return bytes2(name);
}
function changeBytes16() view returns(bytes16){
return bytes16(name);
}
}
pragma solidity ^0.4.0;
contract fixToDynamic{
bytes12 name=0x7a68656e67a69616e787576e;
function fixBytesToDynamicBytes()view returns (bytes){
bytes memory newName=new bytes(name.length);
for(uint i=0;i<name.length;i++){
newName[i]=name[i];
}
return newName;
}
}
5.bytes与string的相互转换
bytes的初始化————new bytes 获取bytes的长度,内容,并修改长度内容
string————不能够直接获取长度和内容,需要转换成bytes 、特殊字符,特别是中文占了3个字节
固定长度字节数组之间的相互转换
固定长度字节数组————bytes可变长度字节数组
将bytes转化为string
pragma solidity ^0.4.0;
contract BytesToString{
bytes name=new bytes(2);
function Init(){
name[0]=0x7a;
name[1]=0x68;
}
function bytesToString()view returns(string){
return string(name);
}
}
将固定的bytes 转化为string
pragma solidity ^0.4.0;
contract Bytes32ToString{
bytes2 name=0x7a68;
function changeIt()returns(string){
// return string(name);
}
function bytes32ToString(bytes32 _newname)view returns(string){
bytes memory newname = new bytes(_newname.length);
for(uint i=0;i<_newname.length;i++){
newname[i]=_newname[i];
}
return string(newname);
}
}
pragma solidity ^0.4.4;
contract Bytes32ToString{
bytes2 name=0x7a68;
function changeIt()returns(string){
// return string(name);
}
function bytes32ToString(bytes32 _newname)view returns(string){
uint count=0;
for(uint i=0;i<_newname.length;i++){
bytes1 char=_newname[i];
if(char !=0){
count++;
}
}
bytes memory newname=new bytes(count);
for(uint j=0;j<count;j++){
newname[j]=_newname[j];
}
return string(newname);
}
}