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   可获取单个字节

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值