solidity 字符串拼凑_以太坊(十三)Solidity数据类型-string、定长字节数组、动态字节数组的关系...

定长字节数组(Fixed-size byte arrays)之间的转换

定长字节数组我们可以通过bytes0 ~ bytes32来进行声明,定长字节数组的长度不可变,内容不可修改。接下来我们通过下面的代码看看定长字节数组的转换关系。

pragma solidity ^0.4.0;

contract C {

bytes10 name10 = 0x77616e6773616e6a756e;

function bytes10ToBytes1() constant returns (bytes1) {

return bytes1(name10);

}

function bytes10ToBytes2() constant returns (bytes2) {

return bytes2(name10);

}

function bytes10ToBytes32() constant returns (bytes32) {

return bytes32(name10);

}

}

QQ20180319-114553@2x.png

结论:当bytes10转bytes1或者bytes2时,会进行低位截断,0x77616e6773616e6a756e转换为bytes1,结果为0x77,转换为bytes2时结果为0x7761。当0x77616e6773616e6a756e转换为bytes32时会进行低位补齐,结果为0x77616e6773616e6a756e00000000000000000000000000000000000000000000。

定长字节数组(Fixed-size byte arrays)转动态字节数组(Dynamically-sized byte array)

pragma solidity ^0.4.0;

contract C {

bytes10 name = 0x77616e6773616e6a756e;

function fixedSizeByteArraysToDynamicallySizedByteArray() constant returns (bytes) {

return bytes(name);

}

}

编译运行时,代码报错:

TypeError: Explicit type conversion not allowed from "bytes10" to "bytes storage pointer".

return bytes(name);

^---------^

结论:定长字节数组和动态字节数组之间不能简单直接转换。

下面是定长字节数组转动态字节数组正确的方式。

pragma solidity ^0.4.0;

contract C {

bytes10 name = 0x77616e6773616e6a756e;

function fixedSizeByteArraysToDynamicallySizedByteArray() constant returns (bytes) {

bytes memory names = new bytes(name.length);

for(uint i = 0; i < name.length; i++) {

names[i] = name[i];

}

return names;

}

}

在上面的代码中,我们根据定长字节数组的长度来创建一个memory类型的动态类型的字节数组,然后通过一个for循环将定长字节数组中的字节按照索引赋给动态字节数组即可。

定长字节数组(Fixed-size byte arrays)不能直接转换为string

pragma solidity ^0.4.0;

contract C {

bytes10 name = 0x77616e6773616e6a756e;

function namesToString() constant returns (string) {

return string(names);

}

}

DeclarationError: Undeclared identifier. Did you mean "name"?

return string(names);

^---^

动态字节数组(Dynamically-sized byte array)转string

重要:因为string是特殊的动态字节数组,所以string只能和动态字节数组(Dynamically-sized byte array)之间进行转换,不能和定长字节数组进行转行。

如果是现成的动态字节数组(Dynamically-sized byte array),如下:

pragma solidity ^0.4.0;

contract C {

bytes names = new bytes(4);

function C() {

names[0] = 0x77;

names[1] = 0x61;

names[2] = 0x6e;

names[3] = 0x67;

}

function namesToString() constant returns (string) {

return string(names);

}

}

QQ20180319-120635@2x.png

如果是定长字节数组转string,那么就需要先将字节数组转动态字节数组,再转字符串

pragma solidity ^0.4.0;

contract C {

function byte32ToString(bytes32 b) constant returns (string) {

bytes memory names = new bytes(b.length);

for(uint i = 0; i < b.length; i++) {

names[i] = b[i];

}

return string(names);

}

}

总结

string本身是一个特殊的动态字节数组,所以它只能和bytes之间进行转换,不能和定长字节数组进行直接转换,如果是定长字节数组,需要将其转换为动态字节数组才能进行转换。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值