Convert String to byte

In this chapter you will learn:

  1. How to decode a string to a byte
  2. How to convert string value to byte value
  3. How to convert string to byte with radix
  4. java.lang.NumberFormatException when byte value is out of range

Decode a string to a byte

decode() method from Byte class accepts decimal, hexadecimal, and octal numbers given by the following grammar:

(+/-)0x for HexDigits                      
(+/-)0X for HexDigits                      
(+/-)# for HexDigits                      
(+/-)0 for OctalDigits

The following code use decode method from Byte class to do the decoding for strings which represents the byte values

public class Main {
  public static void main(String[] args) {
    //from  j  a  v  a2 s.c  o m
    System.out.println("Decimal 10:"+Byte.decode("10"));
    System.out.println("Octal 10:"+Byte.decode("010"));
    System.out.println("Hex F:"+Byte.decode("0XF"));
    
    
    System.out.println("Negative Hex F:"+Byte.decode("-0XF"));
  }
}

The output:

Convert string value to byte value

The following table lists the methods defined in Byte class which we can use to convert string value to byte value.

MethodDescription
static Byte valueOf(String s) Returns a Byte object holding the value given by the specified String.
static Byte valueOf(String s, int radix) Returns a Byte object holding the value extracted from the specified String when parsed with the radix given by the second argument.
static byte parseByte(String s)Parses the string argument as a signed decimal byte.

The following code uses Byte.parseByte to convert string value to byte value.

public class Main {
  public static void main(String[] args) {
    System.out.println("parse string to byte:"+Byte.parseByte("10"));
  }
}

The output:

Convert to byte with radix

Byte.parseByte(String s, int radix) parses the string argument as a signed byte in the radix specified by the second argument and returns a byte value. In the following code we convert string 10 based on radix 8.

public class Main {
  public static void main(String[] args) {
/*from  j a  v  a 2  s .c o  m*/
    System.out.println("parse string to byte:"+Byte.parseByte("10", 8));
  }
}

The output:

And we use the valueOf methods in the same way:

public class Main {
  public static void main(String[] args) {
    /*j  a v a 2  s  .  c  om*/
    System.out.println("parse string to byte:"+Byte.valueOf("10"));
    System.out.println("parse string to byte:"+Byte.valueOf("10", 8));
  }
}

The output:

Value of range

The following code converts the first String value '65' to byte with no problem. When converting "129" to byte java.lang.NumberFormatException is thrown since the 129 is out of the range of a byte value. byte is a signed 8-bit type that has a range from -128 to 127.

public class Main {
/*  j a va 2s .  c o m*/
  public static void main(String[] args) {
    String s = "65";

    byte b = Byte.valueOf(s);

    System.out.println(b);

    // Causes a NumberFormatException since the value is out of range
    System.out.println(Byte.valueOf("129"));
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Create Byte object with its constructor
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing