Solidity 数组类型。定长数组(bytes1)、不定长数组(bytes)与string之间的转换

Dear 丶 2022-02-25 10:08 512阅读 0赞

demo.sol(定长数组,bytes1 … bytes32):

  1. pragma solidity ^0.4.20;
  2. contract fixedArray {
  3. //bytes1 ... bytes32
  4. bytes2 b2 = "xy"; // bytes2 长度必须是2个字符。 定义之后不可以修改。
  5. bytes3 public b3 = "xy"; // bytes3 长度必须是3个字符(后面默认用0补齐)
  6. uint public len = b3.length; // 长度属性只读(不能修改)
  7. bytes8 b8 = "12345678";
  8. bytes1 public b8_1 = b8[0]; // 支持下标。(不能通过下标修改元素)
  9. // b2 = "aa" // 定义之后不可以修改。
  10. }

20190401183056959.png

demo.sol(数组,类型[]):

  1. pragma solidity ^0.4.5;
  2. contract array1Test{
  3. uint len1 = 0 ;
  4. function func() public {
  5. uint[] memory value = new uint[](10); // 定长数组(元素uint类型)。 (memory修饰的局部变量)
  6. value[0] = 1; // 支持下标(可以修改元素值)
  7. //value.push(1); // 不可以添加新元素(长度不能变)
  8. len1 = value.length; // 长度属性
  9. //value.length = 20; // 长度不能修改
  10. }
  11. uint[] public arrayValue; // 不定长数组(元素uint类型)。状态变量(成员变量,默认storage修饰)
  12. uint public len2;
  13. function func2() public {
  14. arrayValue = new uint[](10);
  15. arrayValue[0] = 99; // 支持下标(可以修改元素值)
  16. arrayValue.push(100); // 可以添加新元素(长度可以变)。 也支持pop方法
  17. arrayValue.length = 50; // 长度可以变
  18. len2 = arrayValue.length;
  19. }
  20. uint[7] value = [1,2,3,4,5,6,7]; // 定长数组。
  21. function func3() public {
  22. value[0] = 10; // 支持下标(可以修改元素值)
  23. // value.length = 10; // 长度不可以修改。 不支持push、pop。
  24. }
  25. }

demo.sol(不定长数组,bytes (引用类型)):

  1. pragma solidity ^0.4.4;
  2. contract Test {
  3. bytes public _name = new bytes(1); // 不定长数组。初始长度1
  4. bytes public _name2;
  5. function setLength(uint length) public {
  6. _name.length = length; // 可以修改长度
  7. }
  8. function getLength() constant public returns (uint) {
  9. return _name.length;
  10. }
  11. function setName(bytes name) public {
  12. _name = name; // 数组的值可以修改
  13. }
  14. function changeName(bytes1 name) public {
  15. _name[0] = name; // 可以通过下标修改元素
  16. }
  17. function setInside() public {
  18. _name = "helloWorld";
  19. _name2 = "HELLOWORLD2"; // 可以直接赋值
  20. }
  21. }

demo.sol(不定长数组,string—>bytes (引用类型)):

  1. pragma solidity ^0.4.4;
  2. contract Test {
  3. string public _name = "lily";
  4. function nameBytes() constant public returns (bytes) {
  5. return bytes(_name); // 类型转换 string-->bytes(不定长数组)
  6. }
  7. function nameLength() constant public returns (uint) {
  8. //uint len = _name.length; // string没有length属性
  9. return bytes(_name).length;
  10. }
  11. function changeName() public {
  12. bytes(_name)[0] = "L"; // 会直接作用于(修改)_name字符串
  13. //_name[0] = "H"; //ERROR,不支持下标索引
  14. }
  15. function changeLength() public {
  16. bytes(_name).length = 15; // bytes(不定长数组)
  17. bytes(_name)[14] = "x"; // 会直接作用于(修改)_name字符串
  18. // bytes(_name)[19] = "x"; // ERROR,不能超出下标范围
  19. }
  20. }

20190401183513376.png

demo.sol(定长数组(bytes1)、不定长数组(bytes)、string之间的转换):

  1. pragma solidity ^0.4.20;
  2. contract Test {
  3. bytes10 b10 = 0x68656c6c6f776f726c64; //helloworld 定长数组
  4. //1. 定长字节数组-->变长字节数组 (拷贝)
  5. //bytes bs10 = b10; // ERROR
  6. bytes public bs10 = new bytes(b10.length);
  7. function fixedBytesToBytes() public{
  8. // (拷贝)
  9. for (uint i = 0; i< b10.length; i++){
  10. bs10[i] = b10[i];
  11. }
  12. }
  13. //2. string-->变长字节数组
  14. string greeting = "helloworld";
  15. bytes public b1;
  16. function StringToBytes() public {
  17. b1 = bytes(greeting);
  18. }
  19. //3.变长字节数组-->string
  20. string public str3;
  21. function BytesToString() public {
  22. str3 = string(bs10);
  23. }
  24. //4. 定长字节数组-->string
  25. function FiexBytesToString() public {
  26. //string tmp = string(b10); // ERROR。不可以直接转换,可以通过1.和3.间接转换
  27. }
  28. }

发表评论

表情:
评论列表 (有 0 条评论,512人围观)

还没有评论,来说两句吧...

相关阅读

    相关 vector不定数组

      看书的时候第n次见到vector了,一时兴起:既然这么好用就掌握它吧。 看书看别人的博客发现原理并不复杂。并且!居然支持任意位置插入!!一直以来无限头疼的问题,唯有二叉