Java - Integer toHexString() 方法



描述

Java Integer toHexString() 方法返回整數引數的字串表示形式,該整數引數是以 16 進製表示的無符號整數。

宣告

以下是 java.lang.Integer.toHexString() 方法的宣告

public static String toHexString(int i)

引數

i − 這是要轉換為字串的整數。

返回值

此方法返回引數表示的無符號整數值的十六進位制(基數 16)字串表示形式。

異常

獲取正整數的十六進位制表示示例

以下示例演示瞭如何使用 Integer toHexString() 方法獲取指定 int 值的十六進位制字串表示形式。我們建立了一個 int 變數併為其賦值一個正整數。然後使用 toHexString() 方法列印結果。

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = 170;
      System.out.println("Number = " + i);
    
      /* returns the hexadecimal string representation of the given number */
      System.out.println("toHexString = " + Integer.toHexString(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = 170
toHexString = aa

獲取負整數的十六進位制表示示例

以下示例演示瞭如何使用 Integer toHexString() 方法獲取指定 int 值的十六進位制字串表示形式。我們建立了一個 int 變數併為其賦值一個負整數。然後使用 toHexString() 方法列印結果。

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = -170;
      System.out.println("Number = " + i);
    
      /* returns the hexadecimal string representation of the given number */
      System.out.println("toHexString = " + Integer.toHexString(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = -170
toHexString = ffffff56

獲取正零整數的十六進位制表示示例

以下示例演示瞭如何使用 Integer toHexString() 方法獲取指定 int 值的十六進位制字串表示形式。我們建立了一個 int 變數併為其賦值零值。然後使用 toHexString() 方法列印結果。

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = 0;
      System.out.println("Number = " + i);
    
      /* returns the hexadecimal string representation of the given number */
      System.out.println("toHexString = " + Integer.toHexString(i));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Number = 0
toHexString = 0
java_lang_integer.htm
廣告
© . All rights reserved.