Published: 04 Apr 2018 › Updated: 04 Apr 2018
Solidity中固定大小字节数组转string
string本身是一个特殊的动态字节数组,所以它只能和bytes之间进行转换,不能和固定大小字节数组进行直接转换,如果是固定字节大小数组,需要将其转换为动态字节大小数组才能进行转换。
``
pragma solidity ^0.4.4;
contract C {
function bytes32ToString(bytes32 x) constant returns (string) {
bytes memory bytesString = new bytes(32);
uint charCount = 0;
for (uint j = 0; j < 32; j++) {
byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
if (char != 0) {
bytesString[charCount] = char;
charCount++;
}
}
bytes memory bytesStringTrimmed = new bytes(charCount);
for (j = 0; j < charCount; j++) {
bytesStringTrimmed[j] = bytesString[j];
}
return string(bytesStringTrimmed);
}
function bytes32ArrayToString(bytes32[] data) constant returns (string) {
bytes memory bytesString = new bytes(data.length * 32);
uint urlLength;
for (uint i = 0; i< data.length; i++) {
for (uint j = 0; j < 32; j++) {
byte char = byte(bytes32(uint(data[i]) * 2 ** (8 * j)));
if (char != 0) {
bytesString[urlLength] = char;
urlLength += 1;
}
}
}
bytes memory bytesStringTrimmed = new bytes(urlLength);
for (i = 0; i < urlLength; i++) {
bytesStringTrimmed[i] = bytesString[i];
}
return string(bytesStringTrimmed);
}
}
``
Leave Solidity中固定大小字节数组转string to:
Read more #solidity posts
Best Posts From a186r
We have not curated any of a186r's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From a186r
- homebrew代理设置
- 给git设置代理
- libidn11: /usr/lib/libidn.so.11 exists in filesystem (owned by fcitx-sogoupinyin)
- linux环境下制作镜像[转]
- ubuntu下的加密压缩与解压
- 在ubuntu上安装zsh
- ubuntu 16.04 LTS卸载不常用的软件
- gradle下载超时的问题
- ubuntu下wps中文字体缺失问题
- ubuntu系统备份和恢复(2)
- ubuntu备份和恢复完整系统
- 在终端使用代理
- solidity中的函数修饰符
- 在终端中为git、curl等设置代理
- linux处理tar.xz文件
- linux中使用代理安装dropbox
- Solidity中固定大小字节数组转string
- EOS测试链搭建
- 给linux桌面设置一张实时的地球照片
- git相关配置