Solidity语言学习笔记————14、动态字节数组

构建大小可变的动态数组

数组的大小即可以有固定的,也可以实现动态改变。在不同的场景使用不同的类型才是最正确的选择。

String ——是一个动态尺寸的UTF-8编码字符串,它其实是一个特殊的可变的字节数组,string是引用类型,而非值类型。

bytes——动态字节数组,引用类型。

根据经验,在我们不能够确定字节数据大小的情况下,我们可以使用string或者bytes,而如果我们清楚的知道或者能够将字节控制在byte1~byte32,那么我们就使用byte1~byte32,这样的话能够降低存储空间的消耗。

常规字符串String转换为bytes

String字符串中没有提供length方法获取字符串长度,也没有提供方法修改某个索引的字节码,不过我们可以将String转换为bytes,之后再调用length方法获取字节长度,当然可以修改某个索引的字节码。

源代码如下:

pragma solidity ^0.4.4;
contract test {

    bytes9 public g = 0x6c697975656368756e;
    string public name = "liyuechun";
    function gByteLength() constant returns (uint) {
        return g.length;
    }

    function nameBytes() constant returns (bytes) {
        return bytes(name);
    }

    function nameLength() constant returns (uint) {
        return bytes(name).length;
    }

    function setNameFirstByteForL(bytes1 z) {
        // 0x4c => "L"
        bytes(name)[0] = z;
    }
}

测试效果:

说明

function nameBytes() constant returns (bytes) {
    return bytes(name);
}
namebytes这个函数的功能是将字符串name转换成bytes,并且返回的结果为 0x6c697975656368756e即将字符串转换为对应的16进制数。 0x6c697975656368756e一共有9个字节,也就是一个英文字母对应一个字节。
function nameLength() constant returns (uint) {
    return bytes(name).length;
}

我们之前讲过,string字符串它并不提供length方法帮助我们返回字符串长度,所以在namelength方法中,我们将name转换为bytes,然后再调用length方法来返回字节数,因为一个字节对应一个英文字母,所以返回的字节数量刚好等于这个字符串的长度。

function setNameFirstByteForL(bytes1 z) {
    // 0x4c => "L"
    bytes(name)[0] = z;
}

如果我们想将name字符串中的某个字母进行修改,那么我们直接通过x[k]=z的形式进行修改即可。x是bytes类型的字节数组,k是索引,z是byte1类型的变量值。

setNameFirstByteForL方法中,我们就将Liyuechun中的首字母修改成L,我传入的z的值为0x4c,即大写的L。

扫描二维码关注公众号,回复: 1689802 查看本文章

汉字字符串或特殊字符的字符串转换为bytes

特殊字符
pragma solidity ^0.4.4;

contract test {

    string public name = "a!+&520";
    function nameBytes() constant returns (bytes) {
        return bytes(name);
    }

    function nameLength() constant returns (uint) {
        return bytes(name).length;
    }

}


在这个案例中,我们声明了一个name字符串,值为a!+&520,根据nameBytes和namelength返回的结果中,我们不难看出不管是字母,数字还是特殊字符,每一个字母对应一个byte(字节)。

中文字符串
pragma solidity ^0.4.4;

contract test {
    string public name = "黎跃春";

    function nameBytes() constant returns (bytes) {
        return bytes(name);
    }
    function nameLength() constant returns (uint) {

        return bytes(name).length;
    }

}

在上面的代码中,我们不难看出,黎跃春转换为bytes之后的内容为0xe9bb8ee8b783e698a5一共是9个字节,也就是一个汉字,需要通过3个字节来进行存储。那么问题来了,以后我们取字符串时,字符串中最好不要带汉字,否则计算字符串的长度的时候还需要特殊处理。

创建bytes字节数组

pragma solidity ^0.4.4;

contract test {
    bytes public name = new bytes(1);

    function setNameLength(uint length) {

        name.length = length;
    }
    function nameLength() constant returns (uint) {

        return name.length;
    }

}

Bytes可变数组length和push两个函数的使用实例

pragma solidity ^0.4.4;

contract test {

    // 0x6c697975656368756e
    // 初始化一个两个字节空间的字节数组
    bytes public name = new bytes(2);

    // 设置字节数组的长度
    function setNameLength(uint len) {

        name.length = len;
    }

    // 返回字节数组的长度
    function nameLength() constant returns (uint) {

        return name.length;
    }

    // 往字节数组中添加字节
    function pushAByte(byte b) {

        name.push(b);
    }

}

说明:new bytes(2)的意思是新建一个长度为2的可变数组。当字节数组的长度只有2时,如果你通过push往里面添加了一个字节,那么他的长度就会变为3,当字节数组里面有3个字节,但是你通过length方法将其长度修改为2时,字节数组中最后一个字节将被从字节数组中移除(如果将长度修改为0,那么数组将被清空)。

总结

对比分析:

不可变字节数组:可以用关键词byte来进行声明,但是数组长度、数组内容不可修改

可变字节数组:可以通过bytes name=new bytes(length)来声明可变大小,并且可以修改字节内容的可变数组。



猜你喜欢

转载自blog.csdn.net/fly_hps/article/details/80759022