Solidity string操作

目录

string, bytes, byte32相互转换

1. bytes32 to string/bytes

2.  bytes to string

3. string to bytes

 string to array

比较两个字符串

 拼接两个字符串


string, bytes, byte32相互转换

1. bytes32 to string/bytes

function bytes32ToStr(bytes32 _bytes32) public pure returns (string memory) {
        // string memory str = string(_bytes32);
        // TypeError: Explicit type conversion not allowed from "bytes32" to "string storage pointer"
        // thus we should fist convert bytes32 to bytes (to dynamically-sized byte array)
    
        bytes memory bytesArray = new bytes(32);
        for (uint256 i; i < 32; i++) {
            bytesArray[i] = _bytes32[i];
            }
        return string(bytesArray);
    }
    
    function bytes32Tobytes(bytes32 _bytes32) public pure returns (bytes memory) {
        bytes memory bytesArray = new bytes(32);
        for (uint256 i; i < 32; i++) {
            bytesArray[i] = _bytes32[i];
            }
        return bytesArray;
    }

2.  bytes to string

function bytesToStr(bytes memory _bytes) public pure returns (string memory){
        
        return string(_bytes);
    }

3. string to bytes/bytes32

 function StringToBytesVer1(string memory source)public returns (bytes memory result) {
        return bytes(source);
  }

  function stringToBytesVer2(string memory source)public returns (bytes32 result) {
    assembly {
        result := mload(add(source, 32))
    }
  }

 输入: "0xaf230a56630ab5f081d5fd9d4c4c08c65417e0e49b680b87d287923aa6c025bd"

 注意bytesTostring 和 bytes32Tobytes 是正常的,其它有点异常

 

 

1. 输入"0x32“

查看string, bytes, byte32的内部存储:

 

bytes32会自动在后面补0; bytes32: "0x3200000000000000000000000000000000000000000000000000000000000000", 共64位16进制。

string不做转换; string:  "0x32"

bytes1: 一个字节bytes1:  "0x32"

bytes把"0x32"每个字符转换为字节:bytes: "0x30783332"     ......0->30;   x->78;   3->33;  2->32;都是对应的hex表示

 

2. 输入"0xa"  和  "0xad":

bytes32:   "0x0a00000000000000000000000000000000000000000000000000000000000000"   "0xad00000000000000000000000000000000000000000000000000000000000000"

string:  "0xa"      "0xad"

bytes1: "0x0a"    "0xad"     这里不把0xa-->0x0a.

bytes:   0x307861           bytes: 0x30786164          0x-->3078

 string to array

function ConvertString2Arr(string memory ss, uint one_adr_len) public pure returns(string[] memory){
        //uint one_adr_len=46;
        //the length of one ipfs hash address. eg., QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco
        bytes memory _ss=bytes(ss);
        uint len=_ss.length/one_adr_len;
        string[] memory arr=new string[](len);
        uint k=0;
        for(uint i=0;i<len;i++)
        {
          bytes memory item=new bytes(one_adr_len);
          for(uint j=0; j<one_adr_len; j++)
          {
            item[j]=_ss[k++];
          }
          //emit printbytes(item);
          arr[i]=string(item);
        }
        return arr;
    }

比较两个字符串

function StrCmp(string memory s1, string memory s2)public pure returns(bool){
        bytes memory _s1=bytes(s1);
        bytes memory _s2=bytes(s2);
        uint len=_s1.length;
        for(uint i=0;i<len;i++){
            if(_s1[i]!=_s2[i])
                return false;
        }
        return true;
    }

 拼接两个字符串

function strConcat(string memory _a, string memory _b) internal pure returns (string memory){
        bytes memory _ba = bytes(_a);
        bytes memory _bb = bytes(_b);
        string memory ret = new string(_ba.length + _bb.length);
        bytes memory bret = bytes(ret);
        uint k = 0;
        for (uint i = 0; i < _ba.length; i++)bret[k++] = _ba[i];
        for (uint i = 0; i < _bb.length; i++) bret[k++] = _bb[i];
        return string(ret);
   } 

参考

Solidity 学习笔记(1)- string和bytes   可获取单个字节

 

 

发布了9 篇原创文章 · 获赞 0 · 访问量 858
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览