current position:Home>How to convert byte [] array into string array in solidity?

How to convert byte [] array into string array in solidity?

2022-02-03 20:56:10 Q & A of Denglian community

//pragma solidity ^0.5.1;pragma experimental ABIEncoderV2;contract SearchPeopleInfo{    uint _TransactionIDx;    bytes[]  peoplelocations;    bytes[]  existtime;    struct PeopleInfo{        uint peopleID;        bytes peopleLocation;        bytes ExistTime;    }    mapping(uint => PeopleInfo) public _peopleInfo;    uint[] public _pendingPeopleInfo;    function EnterInfo(uint peopleID, string memory stringpeopleLocation, string memory stringExistTime)public {          uint TransactionID=_TransactionIDx++;        PeopleInfo memory peopleInfo;        peopleInfo.peopleID=peopleID;        peopleInfo.peopleLocation=bytes(stringpeopleLocation);//string to bytes        peopleInfo.ExistTime=bytes(stringExistTime);//string to bytes        _peopleInfo[TransactionID]=peopleInfo;        _pendingPeopleInfo.push(TransactionID);    }    function searchInfo(uint _peopleID)public {        delete peoplelocations;        delete existtime;        for (uint i=0;i<=_pendingPeopleInfo.length;i++){        if (_peopleInfo[i].peopleID==_peopleID){            peoplelocations.push(_peopleInfo[i].peopleLocation);            existtime.push(_peopleInfo[i].ExistTime);        }        }    }    function getInfo( )public view returns (bytes[] memory,bytes[] memory){         return(peoplelocations,existtime);    }}

function getInfo() Return to two bytes[], Respectively peoplelocations as well as existtime, Can it be in solidity Take these two bytes[] Convert to an array of strings string[]?

Take the answer 1:

Bytes It can be converted directly to String: Use string() Converting each element in turn can string[]

Other answers 1:

Other answers 2:

copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.

Random recommended