Convert String To Byte in Java

Converting a String to a byte in Java is a common operation when dealing with data serialization or reading binary data. In Java, a byte data type represents an 8-bit signed two's complement integer, ranging from -128 to 127. In this blog post, we will explore different methods to convert a String to a byte and provide practical examples with outputs to illustrate each approach.

Method 1: Using Byte.parseByte() Method 

The Byte.parseByte() method parses the string argument as a signed byte and returns the resulting byte value.

Example:
public class StringToByteExample {
    public static void main(String[] args) {
        String strNumber = "100";

        try {
            // Convert String to byte using parseByte()
            byte byteValue = Byte.parseByte(strNumber);
            System.out.println("Input String: " + strNumber);
            System.out.println("Byte Value: " + byteValue);
        } catch (NumberFormatException e) {
            System.out.println("Error: Invalid input for byte conversion");
        }
    }
}

Output:

Input String: 100
Byte Value: 100

Method 2: Using Byte.valueOf() Method 

The Byte.valueOf() method converts a String to a Byte object. It returns a Byte instance representing the specified byte value if the String is a valid representation of a byte. 

Example:
public class StringToByteExample {
    public static void main(String[] args) {
        String strNumber = "50";

        try {
            // Convert String to Byte using valueOf()
            Byte byteValue = Byte.valueOf(strNumber);
            System.out.println("Input String: " + strNumber);
            System.out.println("Byte Value: " + byteValue);
        } catch (NumberFormatException e) {
            System.out.println("Error: Invalid input for byte conversion");
        }
    }
}

Output:

Input String: 50
Byte Value: 50

Method 3: Using NumberFormat.parse() Method 

You can also use NumberFormat to parse the String and convert it to a byte. 

Example:
import java.text.NumberFormat;
import java.text.ParseException;

public class StringToByteExample {
    public static void main(String[] args) {
        String strNumber = "77";

        // Convert String to Byte using NumberFormat
        try {
            NumberFormat numberFormat = NumberFormat.getInstance();
            byte byteValue = numberFormat.parse(strNumber).byteValue();
            System.out.println("Input String: " + strNumber);
            System.out.println("Byte Value: " + byteValue);
        } catch (ParseException e) {
            System.out.println("Error: Invalid input for byte conversion");
        }
    }
}

Output:

Input String: 77
Byte Value: 77

Conclusion

Converting a String to a byte is a straightforward process in Java. You can use methods like Byte.parseByte(), Byte.valueOf(), or NumberFormat to achieve this. In this blog post, we provided practical examples with outputs to demonstrate each method's effectiveness. Always handle exceptions properly to handle invalid input gracefully and ensure the reliability of your code.

Comments