pragma solidity ^0.4.0;
//固定字节数组与String相互转化
contract BytesToString{
bytes2 name = 0x6368;
function changeIt() public returns(string){
}
// return string(name);不可以不可以不可以
function bytes32Tostring(bytes32 _newname) public view returns(string){
bytes memory newname = new bytes(_newname.length);
for(uint i = 0; i < _newname.length;i++){
newname[i] = _newname[i];
}
return string(newname);
}
function bytes32Tostring2(bytes2 _newname) public view returns(string){
uint count = 0;
for(uint i = 0; i < _newname.length;i++){
bytes1 char = _newname[i];
if(char != 0 ){
count++;
}
}
bytes memory newname = new bytes(count);
for(uint j = 0;j <count;j++){
newname[j] = _newname[j];
}
return string(newname);
}
}
10.Solidity-固定字节数组与String相互转化
于 2022-11-06 17:10:20 首次发布