Java – String to Byte conversion in 3 ways

In this article, we will discuss various ways to convert String to Byte in Java

1. Byte:

  • Size is 1 byte
  • Its range is -128 to 127

2. Various ways to convert String to Byte:

  1. using Byte.parseByte(“strValue”);
  2. Using Boolean.valueOf(“strValue”);
  3. Create Byte object and pass string as constructor-argument

Read String class in detail with example

Let us move forward and discuss all possible ways to convert String to Byte in Java

2.1 Using Byte.parseByte(“strValue”); method

  • This method can be used to convert String into primitive byte data-type
  • Note: The value range should be within -128 to 127

Method signature:

public static byte parseByte(String s) throws NumberFormatException;

ConvertStringIntoByteUsingParseByteMethod.java

package in.bench.resources.string.to.bytes.conversion;

public class ConvertStringIntoByteUsingParseByteMethod {

	public static void main(String[] args) {

		// String with only digits within byte range
		String str1 = "100";

		// 1. converting String to byte
		byte byteValue1 = Byte.parseByte(str1);
		System.out.println("1. Converted byte value is : "
				+ byteValue1);

		// String with only digits within byte range (-)
		String str2 = "-99";

		// 2. converting String to byte
		int byteValue2 = Byte.parseByte(str2);
		System.out.println("\n2. Converted byte value is : "
				+ byteValue2);
	}
}

Output:

1. Converted byte value is : 100

2. Converted byte value is : -99

2.2 Using Byte.valueOf(“strValue”); method

  • This method can be used to convert String into Byte wrapper-type
  • Note: The value range should be within -128 to 127

Method signature:

public static Byte valueOf(String s) throws NumberFormatException;

ConvertStringIntoByteUsingValueOfMethod.java

package in.bench.resources.string.to.bytes.conversion;

public class ConvertStringIntoByteUsingValueOfMethod {

	public static void main(String[] args) {

		// String with only digits within byte range
		String str1 = "54";

		// 1. converting String to Byte
		Byte byteValue1 = Byte.valueOf(str1);
		System.out.println("1. Converted Byte value is : "
				+ byteValue1);

		// String with only digits within byte range (-)
		String str2 = "-97";

		// 2. converting String to Byte
		Byte byteValue2 = Byte.valueOf(str2);
		System.out.println("\n2. Converted Byte value is : "
				+ byteValue2);
	}
}

Output:

1. Converted Byte value is : 54

2. Converted Byte value is : -97

2.3 Create Byte object and pass string as constructor-argument

  • Here, we will create new Byte object with String as constructor-argument
  • After creating new Byte object by passing string value, then invoke byteValue() method for converting String to primitive byte data-type
  • Passed string to constructor argument should constitutes of only numbers (or digits)
  • Note: The value range should be within -128 to 127

Method signature:

public Byte(String s) throws NumberFormatException;

public byte byteValue();

ConvertStringToByteUsingByteValueMethod.java

package in.bench.resources.string.to.bytes.conversion;

public class ConvertStringToByteUsingByteValueMethod {

	public static void main(String[] args) {

		// String with only digits within byte range
		String str1 = "102";

		// 1. converting String to Byte
		// by creating new Byte Object
		Byte byte1 = new Byte(str1);
		byte byteValue1 = byte1.byteValue();
		System.out.println("1. Converted Byte value is : "
				+ byteValue1);
		

		// String with only digits within byte range (-)
		String str2 = "-127";

		// 2. converting String to Byte
		// by creating new Byte Object
		Byte byte2 = new Byte(str2);
		byte byteValue2 = byte2.byteValue();
		System.out.println("\n2. Converted Byte value is : "
				+ byteValue2);
	}
}

Output:

1. Converted Byte value is : 102

2. Converted Byte value is : -127

2.4 Exception scenarios:

  • In first 3 cases, whenever string isn’t properly formatted to convert String to Byte, then NumberFormatException will be thrown
  • We will see, what happens if string isn’t correctly formatted for all 3 ways of conversion
  • For case 4 i.e.; E.4, we will convert out of range value for byte

3. Exception scenario:

  • handle exception properly for all 3 ways
  • otherwise NumberFormatException will be thrown for incorrect string values (or not correctly formatted string)
  • and terminates program abruptly
  • we will see 3 different examples for all cases with E.1, E.2 and E.3
  • We will also see one example for out of range value for Byte with case E.4

3.1 Handle NumberFormatException while converting String to byte using Byte.parseByte() method

ParseByteMethodThrowsNFE.java

package in.bench.resources.string.to.bytes.conversion;

public class ParseByteMethodThrowsNFE {

	public static void main(String[] args) {

		// String with only digits within byte range
		String str1 = "10be";

		// 1. converting String to byte
		byte byteValue1 = Byte.parseByte(str1);
		System.out.println("1. Converted byte value is : "
				+ byteValue1);
		

		// String with only digits within byte range (-)
		String str2 = "-99res";

		// 2. converting String to byte
		int byteValue2 = Byte.parseByte(str2);
		System.out.println("\n2. Converted byte value is : "
				+ byteValue2);
	}
}

Output:

Exception in thread "main" java.lang.NumberFormatException:
For input string: "10be"
at java.lang.NumberFormatException.forInputString(
NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Byte.parseByte(Byte.java:149)
at java.lang.Byte.parseByte(Byte.java:175)
at in.bench.resources.string.to.bytes.conversion
.ParseByteMethodThrowsNFE
.main(ParseByteMethodThrowsNFE.java:11)

3.2 Handle NumberFormatException while converting String to Byte using Byte.valueOf() method

ByteValueOfMethodThrowsNFE.java

package in.bench.resources.string.to.bytes.conversion;

public class ByteValueOfMethodThrowsNFE {

	public static void main(String[] args) {

		// String with only digits within byte range
		String str1 = "54ben";

		// 1. converting String to Byte
		Byte byteValue1 = Byte.valueOf(str1);
		System.out.println("1. Converted Byte value is : "
				+ byteValue1);
		

		// String with only digits within byte range (-)
		String str2 = "-97res";

		// 2. converting String to Byte
		Byte byteValue2 = Byte.valueOf(str2);
		System.out.println("\n2. Converted Byte value is : "
				+ byteValue2);
	}
}

Output:

Exception in thread "main" java.lang.NumberFormatException:
For input string: "54ben"
at java.lang.NumberFormatException.forInputString(
NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Byte.parseByte(Byte.java:149)
at java.lang.Byte.valueOf(Byte.java:205)
at java.lang.Byte.valueOf(Byte.java:231)
at in.bench.resources.string.to.bytes.conversion
.ByteValueOfMethodThrowsNFE
.main(ByteValueOfMethodThrowsNFE.java:11)

3.3 Handle NumberFormatException while converting String to Byte object using byteValue() method

ByteValueMethodThrowsNFE.java

package in.bench.resources.string.to.bytes.conversion;

public class ByteValueMethodThrowsNFE {

	public static void main(String[] args) {

		// String with only digits within byte range
		String str1 = "1ben";

		// 1. converting String to Byte
		// by creating new Byte Object
		Byte byte1 = new Byte(str1);
		byte byteValue1 = byte1.byteValue();
		System.out.println("1. Converted Byte value is : "
				+ byteValue1);
		

		// String with only digits within byte range (-)
		String str2 = "-12res7";

		// 2. converting String to Byte
		// by creating new Byte Object
		Byte byte2 = new Byte(str2);
		byte byteValue2 = byte2.byteValue();
		System.out.println("\n2. Converted Byte value is : "
				+ byteValue2);
	}
}

Output:

Exception in thread "main" java.lang.NumberFormatException:
For input string: "1ben"
at java.lang.NumberFormatException.forInputString(
NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Byte.parseByte(Byte.java:149)
at java.lang.Byte.<init>(Byte.java:316)
at in.bench.resources.string.to.bytes.conversion
.ByteValueMethodThrowsNFE
.main(ByteValueMethodThrowsNFE.java:11)

3.4 For out of range value for Byte i.e.; outside prescribed range of -128 to 127

ByteOutOfRangeValueException.java

package in.bench.resources.string.to.bytes.conversion;

public class ByteOutOfRangeValueException {

	public static void main(String[] args) {

		// String with only digits within byte range
		String str1 = "130";

		// 1. converting String to byte
		byte byteValue1 = Byte.parseByte(str1);
		System.out.println("1. Converted byte value is : "
				+ byteValue1);
		

		// String with only digits within byte range (-)
		String str2 = "-129";

		// 2. converting String to byte
		int byteValue2 = Byte.parseByte(str2);
		System.out.println("\n2. Converted byte value is : "
				+ byteValue2);
	}
}

Output:

Exception in thread "main" java.lang.NumberFormatException:
Value out of range. Value:"130" Radix:10
at java.lang.Byte.parseByte(Byte.java:151)
at java.lang.Byte.parseByte(Byte.java:175)
at in.bench.resources.string.to.bytes.conversion
.ByteOutOfRangeValueException
.main(ByteOutOfRangeValueException.java:11)

4. Auto-boxing of Byte wrapper-type to primitive byte data-type and vice-versa

  • Auto-boxing feature available from Java 1.5 version
  • So, converting primitive data-type to wrapper-type can easily be done, by directly assigning
  • Let’s see one example based on this auto-boxing feature

AutoBoxingFeatureForByteConversion.java

package in.bench.resources.string.to.bytes.conversion;

public class AutoBoxingFeatureForByteConversion {

	public static void main(String[] args) {

		// String with only digits within byte range
		String str1 = "101";

		// String to byte conversion
		byte byteValue1 = Byte.parseByte(str1);

		// 1. Auto-Boxing - converting byte to Byte
		Byte byteAutoBoxing = byteValue1;
		System.out.println("1. Auto-Boxing : "
				+ byteAutoBoxing);


		// String with only digits within byte range (-)
		String str2 = "-95";

		// String to Byte conversion
		Byte byteValue2 = Byte.valueOf(str2);

		// 2. Un-Boxing - converting Byte to byte
		byte byteUnBoxing = byteValue2;
		System.out.println("\n2. Un-Boxing   : "
				+ "" + byteUnBoxing);
	}
}

Output:

1. Auto-Boxing : 101

2. Un-Boxing   : -95

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Byte to String conversion in 5 ways
Java - Character[] array to String conversion in 5 ways