Java.lang.Byte.decode() 方法
描述
java.lang.Byte.decode(String nm) 将字符串解码为字节。 接受以下语法给出的十进制、十六进制和八进制数 −
可解码字符串
- Signopt DecimalNumeral
- Signopt 0x HexDigits
- Signopt 0X HexDigits
- Signopt # HexDigits
- Signopt 0 OctalDigits
签名
+
−
在可选符号和/或基数说明符("0x"、"0X"、"#"或前导零)之后的字符序列被 Byte.parseByte 方法解析为具有指示的基数(10, 16 或 8)。
此字符序列必须表示正值,否则将引发 NumberFormatException。 如果指定字符串的第一个字符是减号,则结果为否定。 字符串中不允许有空格字符。
声明
以下是 java.lang.Byte.decode() 方法的声明。
public static Byte decode(String nm)throws NumberFormatException
参数
nm − 要解码的字符串
返回值
该方法返回一个 Byte 对象,该对象保存了 nm 表示的字节值。
异常
NumberFormatException − 如果字符串不包含可解析字节
示例
下面的例子展示了 lang.Byte.decode() 方法的使用。
package com.tutorialspoint;
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

