欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

java 16进制整数,Java将整数转换为十六进制整数

发布时间:2025/3/11 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java 16进制整数,Java将整数转换为十六进制整数 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

I'm trying to convert a number from an integer into an another integer which, if printed in hex, would look the same as the original integer.

For example:

Convert 20 to 32 (which is 0x20)

Convert 54 to 84 (which is 0x54)

解决方案

public static int convert(int n) {

return Integer.valueOf(String.valueOf(n), 16);

}

public static void main(String[] args) {

System.out.println(convert(20)); // 32

System.out.println(convert(54)); // 84

}

That is, treat the original number as if it was in hexadecimal, and then convert to decimal.

总结

以上是生活随笔为你收集整理的java 16进制整数,Java将整数转换为十六进制整数的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。