我的整数值为1238,当我使用Integer.toHexString函数转换为十六进制字符串时,它将返回4d6。有什么可能的方式格式化,以便它返回04d6?如果您能提供一般情况的解决方案,而不是此特定情况的解决方案,那就太好了。谢谢你。
如果您能提供一般情况的解决方案,而不是此特定情况的解决方案,那将是很好的
这是一个快速的解决方案,在十六进制字符串长度不是2的倍数的情况下,将向左添加0。
StringBuilder sb = new StringBuilder();
sb.append(Integer.toHexString(myInt));
if (sb.length() % 2 > 0) {
sb.insert(0, '0'); // pad with leading zero if needed
}
String hex = sb.toString();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句