Facebook Page Twitter Page LinkedIn Page
× Java.lang Package Classes


The java.lang.Byte.decode() method is used to decode a String into a Byte. Accepts decimal, hexadecimal, and octal numbers given by the following grammar:

DecodableString:

  • Signopt DecimalNumeral
  • Signopt 0x HexDigits
  • Signopt 0X HexDigits
  • Signopt # HexDigits
  • Signopt 0 OctalDigits

Sign:

  • +
  • -

The sequence of characters following an optional sign and/or radix specifier ("0x", "0X", "#", or leading zero) is parsed as by the Byte.parseByte method with the indicated radix (10, 16, or 8). This sequence of characters must represent a positive value or a NumberFormatException will be thrown. The result is negated if first character of the specified String is the minus sign. No whitespace characters are permitted in the String.

Syntax

public static Byte decode(String nm)
                   throws NumberFormatException

Parameters

nm Specify the String to decode.

Return Value

Returns a Byte object holding the byte value represented by nm.

Exception

throws NumberFormatException, if the String does not contain a parsable byte.

Example:

In the below example, the java.lang.Byte.decode() method is used to decode a String into a Byte.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating a string holding byte value
    String x1 = "25";    //decimal number
    String x2 = "0x6f";  //hexadecimal number
    String x3 = "0X6B";  //hexadecimal number
    String x4 = "-#6c";  //hexadecimal number
    String x5 = "027";   //octal number

    //creating Byte object
    Byte y1 = Byte.decode(x1);
    Byte y2 = Byte.decode(x2);
    Byte y3 = Byte.decode(x3);
    Byte y4 = Byte.decode(x4);
    Byte y5 = Byte.decode(x5);

    //printing the Byte object 
    System.out.println("The Byte object y1 is: " + y1);
    System.out.println("The Byte object y2 is: " + y2);  
    System.out.println("The Byte object y3 is: " + y3);  
    System.out.println("The Byte object y4 is: " + y4);  
    System.out.println("The Byte object y5 is: " + y5);     
  }
}

The output of the above code will be:

The Byte object y1 is: 25
The Byte object y2 is: 111
The Byte object y3 is: 107
The Byte object y4 is: -108
The Byte object y5 is: 23

❮ Java.lang - Byte