java.lang.Byte.decode()方法實例
java.lang.Byte.decode(String nm) 解碼字符串轉換爲字節。接受十進制,十六進制,並通過以下語法給出八進制數:
解碼字符串
Signopt DecimalNumeral
Signopt 0x HexDigits
Signopt 0X HexDigits
Signopt # HexDigits
Signopt 0 OctalDigits
SIGN
+
−
字符以下的可選符號和/或基數說明符(「0x」,「0X」,「#」,或前導零),該序列被解析爲受Byte.parseByte方法與指示的基數(10,16,或8)。
此字符序列必須代表一個正值,否則將拋出NumberFormatException。如果指定的字符串的第一個字符是減號,結果是負數。任何空白字符被允許在字符串。
聲明
以下是java.lang.Byte.decode()方法的聲明
public static Byte decode(String nm)throws NumberFormatException
參數
- nm - 字符串進行解碼
返回值
此方法將返回一個字節對象持有nm的所代表的字節值。
異常
- NumberFormatException - 如果該字符串不包含一個可分析的字節
例子
下面的例子顯示了lang.Byte.decode()方法的使用。
package com.yiibai; import java.lang.*; public class ByteDemo { public static void main(String[] args) { // create 4 Byte objects Byte b1, b2, b3, b4; /**
* static methods are called using class name.
* decimal value is decoded and assigned to Byte object b1
*/ b1 = Byte.decode("100"); // hexadecimal values are decoded and assigned to Byte objects b2, b3 b2 = Byte.decode("0x6b"); b3 = Byte.decode("-#4c"); // octal value is decoded and assigned to Byte object b4 b4 = Byte.decode("0127"); String str1 = "Byte value of decimal 100 is " + b1; String str2 = "Byte value of hexadecimal 6b is " + b2; String str3 = "Byte value of hexadecimal -4c is " + b3; String str4 = "Byte value of octal 127 is " + b4; // print b1, b2, b3, b4 values System.out.println( str1 ); System.out.println( str2 ); System.out.println( str3 ); System.out.println( str4 ); } }
讓我們來編譯和運行上面的程序,這將產生以下結果:
Byte value of decimal 100 is 100 Byte value of hexadecimal 6b is 107 Byte value of hexadecimal -4c is -76 Byte value of octal 127 is 87