當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Integer toHexString()用法及代碼示例

Integer類toHexString()方法

  • toHexString() 方法可在java.lang包。
  • toHexString() 方法用於將給定參數 [value] 的整數類型的十六進製字符串表示為基數為 16 的無符號整數。
  • toHexString() 方法是一個靜態方法,它也可以通過類名訪問,如果我們嘗試使用類對象訪問該方法,那麽我們也不會收到錯誤。
  • toHexString() 方法從整數轉換為十六進製字符串時不拋出異常。

用法:

    public static String toHexString (int value);

參數:

  • int value– 表示要轉換的整數值。

返回值:

這個方法的返回類型是int,它返回表示無符號整數值的給定參數的十六進製字符串。

例:

// Java program to demonstrate the example 
// of toHexString (int value) method of Integer class

public class ToHexStringOfIntegerClass {
    public static void main(String[] args) {
        // Variables initialization
        int i1 = 10;
        int i2 = 20;
        int i3 = 30;
        int i4 = Integer.MAX_VALUE;
        int i5 = Integer.MIN_VALUE;

        // Integer instance creation
        Integer value = new Integer(i1);

        // It represents hexadecimal string of the given
        // integer type i2 argument
        String s = value.toHexString(i2);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i2):" + s);

        // It represents hexadecimal string of the given
        // integer type i3 argument
        s = value.toHexString(i3);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i3):" + s);

        // It represents hexadecimal string of the given
        // integer type i4 argument
        s = value.toHexString(i4);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i4):" + s);

        // It represents hexadecimal string of the given
        // integer type i5 argument
        s = value.toHexString(i5);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(i5):" + s);
    }
}

輸出

value.toHexString(i2):14
value.toHexString(i3):1e
value.toHexString(i4):7fffffff
value.toHexString(i5):80000000


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java Integer class toHexString() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。