Java Convert integer to hex integer

前端 未结 8 1695
醉话见心
醉话见心 2020-12-01 09:58

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:

C

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 10:50

    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.

提交回复
热议问题