Java将整数转换为十六进制整数 [英] Java Convert integer to hex integer

查看:579
本文介绍了Java将整数转换为十六进制整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个数字从一个整数转换为另一个整数,如果以十六进制打印,整数将看起来与原始整数相同。



例如:
$ b $ p将20转换为32(即0x20)

将54转换为84(即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
}

即将原始数字视为十六进制,然后转换为十进制。


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将整数转换为十六进制整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆