目录

static Byte decode(String nm)

描述 (Description)

java.lang.Byte.decode(String nm)将String解码为Byte。 接受以下语法给出的十进制,十六进制和八进制数 -

可解码字符串

  • 签署opt DecimalNumeral
  • 签署opt 0x HexDigits
  • 签署选择 0X HexDigits
  • 签署opt #HexDigits
  • 签署选择 0 OctalDigits

Sign

  • +

可选符号和/或基数说明符(“0x”,“0X”,“#”或前导零)后面的字符序列由Byte.parseByte方法解析,并带有指示的基数(10,16或8) )。

此字符序列必须表示正值或将抛出NumberFormatException。 如果指定String的第一个字符是减号,则结果为否定。 字符串中不允许使用空格字符。

声明 (Declaration)

以下是java.lang.Byte.decode()方法的声明

public static Byte decode(String nm)throws NumberFormatException

参数 (Parameters)

nm - 要解码的字符串

返回值 (Return Value)

此方法返回一个Byte对象,其中包含由nm表示的字节值。

异常 (Exception)

NumberFormatException - 如果String不包含可解析的字节

例子 (Example)

以下示例显示了lang.Byte.decode()方法的用法。

package com.iowiki;
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
↑回到顶部↑
WIKI教程 @2018