Java - Byte decode() method



Description

The Java Byte decode(String nm) method decodes a String into a Byte. Accepts decimal, hexadecimal, and octal numbers given by the following grammar −

Decodable String

  • 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.

Declaration

Following is the declaration for java.lang.Byte.decode() method

public static Byte decode(String nm)throws NumberFormatException

Parameters

nm − the String to decode

Return Value

This method returns a Byte object holding the byte value represented by nm.

Exception

NumberFormatException − if the String does not contain a parsable byte

Example 1

The following example shows the usage of Byte decode() method with Byte object. Here we've created a Byte variable and using decode method, we're decoding a decimal string to byte and then result is printed.

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a Byte objects
      Byte b1;

      /**
       *  static methods are called using class name. 
       *  decimal value is decoded and assigned to Byte object b1
       */
      b1 = Byte.decode("100");

      // print value
      System.out.println( "Byte value of decimal 100 is " + b1 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Byte value of decimal 100 is 100

Example 2

The following example shows the usage of Byte decode() method with Byte object. Here we've created a Byte variable and using decode method, we're decoding a hexadecimal string to byte and then result is printed.

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a Byte objects
      Byte b1;

      /**
       *  static methods are called using class name. 
       *  hexadecimal value is decoded and assigned to Byte object b1
       */
      b1 = Byte.decode("0x6b");

      // print value
      System.out.println( "Byte value of hexadecimal 6b is " + b1 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Byte value of hexadecimal 6b is 107

Example 3

The following example shows the usage of Byte decode() method with Byte object. Here we've created a Byte variable and using decode method, we're decoding an octal string to byte and then result is printed.

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a Byte objects
      Byte b1;

      /**
       *  static methods are called using class name. 
       *  octal value is decoded and assigned to Byte object b1
       */
      b1 = Byte.decode("0127");

      // print value
      System.out.println( "Byte value of octal 127 is " + b1 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Byte value of octal 127 is 87
java_lang_byte.htm
Advertisements