博客
关于我
Android/TextView 16进制显示,加0x头,不足2位补0
阅读量:664 次
发布时间:2019-03-15

本文共 432 字,大约阅读时间需要 1 分钟。

首先,通过ToString方法将进度值转化为16进制字符串表示:var str = Integer.toHexString(progress);然后,将转换后的字符串转换回整数值并获取其字节表示:// 错误的写法:直接将字符串转换为字节可能会导致错误// orderArr[3] = str.toInt(16).toByte();orderArr[3] = Integer.parseInt(str, 16).toByte();最后,根据字符串长度决定显示格式:msg.text = (str.length > 1) ? "0x$str" : "0x0$str";
  • 以上代码片段展示了如何将10进制数转换为16进制字符串,并将其反转化为字节值。
  • 注意:Integer.parseInt方法用于在指定进制下解析字符串。
  • 错误写法:直接使用str.toInt(16)可能会导致与预期值不符的结果。
  • 另外,字符串长度为1时,为了保持格式统一可以在前面补充一个"0x0"。

转载地址:http://hxxmz.baihongyu.com/

你可能感兴趣的文章
Python2 已终结,入手Python 3,你需要这30个技巧
查看>>
python写下拉列表在excel,无法使用python获取Excel下拉列表(组合框)值
查看>>
python2.7安装
查看>>
Python2.7安装ncmbot时提示:Microsoft Visual C++9.0 is required
查看>>
PYTHON2.Git.1
查看>>
python20181211
查看>>
Python内存管理机制
查看>>
Python2中range 和xrange的区别??
查看>>
python2和3的区别
查看>>
python2和python3的pip版本升级
查看>>
python2和python3的区别
查看>>
python2备份交换机配置脚本
查看>>
Python3 - 在字符串格式化程序参数中使用变量
查看>>
Python3 A*寻路算法实现
查看>>
python3 grpc安装
查看>>
python3 mysql 封装多表_python3 mysql-多表查询
查看>>
python3 mysql8_Python MySQL8.0 错误:authentication plugin 'calling_sha2_password' is not supported....
查看>>
Python关键代码总览!从Zero到Hero必看的考点
查看>>
python3 robotframework+pycharm框架搭建
查看>>
Python3 shutil模块
查看>>