在《Solidity中uint转bytes》中,我们知道unit如何转换成bytes,其实把uint转换成string,就是在最后加上string(bytes变量)即可,如下所示:

pragma solidity ^0.4.2;

contract Test {

    function toBytesNickJohnson(uint256 x) constant returns (bytes b) {
b = new bytes(32);
assembly { mstore(add(b, 32), x) }
} function getStr(uint playChoice) returns (string s) {
bytes memory c = toBytesNickJohnson(playChoice);
return string(c);
} function toBytesEth(uint256 x) constant returns (bytes b) {
b = new bytes(32);
for (uint i = 0; i < 32; i++) {
b[i] = byte(uint8(x / (2**(8*(31 - i)))));
}
} function toBytesNicolasMassart(uint256 x) constant returns (bytes c) {
bytes32 b = bytes32(x);
c = new bytes(32);
for (uint i=0; i < 32; i++) {
c[i] = b[i];
}
}
}

  但是呢,我在这里再附加一种Solidity中uint转string方法,如下所示:

pragma solidity ^0.4.2;

contract Test {

    function uint2str(uint i) internal returns (string c) {
if (i == 0) return "0";
uint j = i;
uint length;
while (j != 0){
length++;
j /= 10;
}
bytes memory bstr = new bytes(length);
uint k = length - 1;
while (i != 0){
bstr[k--] = byte(48 + i % 10);
i /= 10;
}
c = string(bstr);
}
}

最新文章

  1. iOS之There was an internal API error错误
  2. 查找树ADT——二叉搜索树
  3. Currency System in Geraldion
  4. 标签中id和name的作用和区别
  5. C++Builder设置完BorderStyle的值为none,以后如何实现窗口的移动和拉伸
  6. cmder
  7. ExtJs 使用点滴 十四 通过设置CheckboxSelectionModel属性值来实现GridPanel复选框可用不可用
  8. 后勤数据源增量队列Delta Queue(RSA7)中的增量更新区Delta Update、增量重复区Delta Repetition
  9. inconsistent line count calculation in projection snapshot
  10. hostapd源代码分析(三):管理帧的收发和处理
  11. 关于着色器LinearGradient的使用
  12. Tcl在Vivado中的使用
  13. d3d纹理参数
  14. NodeJs REPL交互式解析器常用命令
  15. springBoot系列教程07:异常捕获
  16. 《前端之路》之四 JavaScript 的闭包、作用域、作用域链
  17. SpringBoot主程序注解@SpringBootApplication简单分析
  18. Filebeat工作过程(二)
  19. 在服务器上同时启动多个tomcat
  20. C# 获取接口数据(xml格式)转为json格式

热门文章

  1. AJAX心得
  2. centos平台基于snort、barnyard2以及base的IDS(入侵检测系统)的搭建与测试及所遇问题汇总
  3. WEB框架本质和第一个Django实例
  4. centos7通过yum安装JDK1.8
  5. Python_day6
  6. myeclipse 自动部署web项目(自动编译)
  7. 11.5 vmstat:虚拟内存统计
  8. CUDA并行编程思维过程
  9. Eclipse 中 Java 代码报版本错误的问题
  10. go操作redis