Java - Byte parseByte() 方法



描述

Java Byte parseByte(String s, int radix) 方法將字串引數解析為第二個引數指定的基數中的帶符號位元組。

字串中的字元必須全部為指定基數的數字(由 Character.digit(char, int) 返回非負值決定),但第一個字元可以是 ASCII 減號 '−' ('\u002D') 表示負值,或 ASCII 加號 '+' ('\u002B') 表示正值。返回生成的位元組值。

如果出現以下任何情況,則會丟擲 NumberFormatException 型別的異常:

  • 第一個引數為空或長度為零的字串。

  • 基數小於 Character.MIN_RADIX 或大於 Character.MAX_RADIX。

  • 字串的任何字元都不是指定基數的數字,但第一個字元可以是減號 '−' ('\u002D') 或加號 '+' ('\u002B'),前提是字串長度大於 1。

  • 字串表示的值不是 byte 型別的值。

宣告

以下是 java.lang.Byte.parseByte() 方法的宣告

public static byte parseByte(String s, int radix)
   throws NumberFormatException

引數

  • s − 包含要解析的位元組表示形式的字串

  • radix − 解析 s 時使用的基數

返回值

此方法返回由指定基數的字串引數表示的位元組值。

異常

NumberFormatException − 如果字串不包含可解析的位元組。

將包含八進位制值的字串解析為位元組的示例

以下示例演示了 Byte parseByte() 方法的使用,用於解析具有八進位制值的字串。我們建立了兩個位元組變數和兩個具有有效值的字串變數。然後使用 parseByte() 方法,我們透過將字串解析為八進位制來獲取位元組,然後列印結果。

package com.tutorialspoint;

public class ByteDemo {
   public static void main(String[] args) {

      // create 2 byte primitives bt1, bt2
      byte bt1, bt2;

      // create and assign values to String's s1, s2
      String s1 = "+123";
      String s2 = "-12";

      /**
       *  static method is called using class name. 
       *  assign parseByte result on s1, s2 to bt1, bt2
       */
      bt1 = Byte.parseByte(s1, 8);
      bt2 = Byte.parseByte(s2, 8);

      String str1 = "Parse byte value of " + s1 + " is " + bt1;
      String str2 = "Parse byte value of " + s2 + " is " + bt2;

      // print bt1, bt2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

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

Parse byte value of +123 is 83
Parse byte value of -12 is -10

將包含十六進位制值的字串解析為位元組的示例

以下示例演示了 Byte parseByte() 方法的使用,用於解析具有十六進位制值的字串。我們建立了兩個位元組變數和兩個具有有效值的字串變數。然後使用 parseByte() 方法,我們透過將字串解析為十六進位制值來獲取位元組,然後列印結果。

package com.tutorialspoint;

public class ByteDemo {
   public static void main(String[] args) {

      // create 2 byte primitives bt1, bt2
      byte bt1, bt2;

      // create and assign values to String's s1, s2
      String s1 = "12";
      String s2 = "-1a";

      /**
       *  static method is called using class name. 
       *  assign parseByte result on s1, s2 to bt1, bt2
       */
      bt1 = Byte.parseByte(s1, 16);
      bt2 = Byte.parseByte(s2, 16);

      String str1 = "Parse byte value of " + s1 + " is " + bt1;
      String str2 = "Parse byte value of " + s2 + " is " + bt2;

      // print bt1, bt2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

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

Parse byte value of 12 is 18
Parse byte value of -1a is -26

解析包含無效十六進位制值的字串以獲取位元組時出現異常的示例

以下示例演示了 Byte parseByte() 方法的使用,用於解析具有無效十六進位制值的字串。我們建立了兩個位元組變數和兩個字串變數,其值為無效值,因為值超出範圍。然後,當我們嘗試使用 parseByte() 方法獲取位元組時,將丟擲 NumberFormatException。

package com.tutorialspoint;

public class ByteDemo {
   public static void main(String[] args) {

      // create 2 byte primitives bt1, bt2
      byte bt1, bt2;

      // create and assign values to String's s1, s2
      String s1 = "123";
      String s2 = "14";

      /**
       *  static method is called using class name. 
       *  assign parseByte result on s1, s2 to bt1, bt2
       */
      bt1 = Byte.parseByte(s1, 16);
      bt2 = Byte.parseByte(s2, 16);
      String str1 = "Parse byte value of " + s1 + " is " + bt1;
      String str2 = "Parse byte value of " + s2 + " is " + bt2;

      // print bt1, bt2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

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

Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"123" Radix:16
	at java.base/java.lang.Byte.parseByte(Byte.java:154)
	at com.tutorialspoint.ByteDemo.main(ByteDemo.java:18)
java_lang_byte.htm
廣告
© . All rights reserved.