Byte to String conversion in Java – 5 ways

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

 

Byte:

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

 

Various ways to convert Byte to String

  1. using Byte.toString(byteVal);
  2. using String.valueOf(byteVal);
  3. create Byte object and then convert to String using toString() method {new Byte(byteVal).toString();}
  4. adding double quotes (“”) to byte value {i.e.; “” + byteVal;}
  5. using append() method of StringBuffer or StringBuilder

Read String class in detail with example

 

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

Way 1: Using Byte.toString(byteVal); method

  • This method can be used to convert primitive byte data-type or Byte wrapper-type to String object

Method signature:

public static String toString(byte b);

ConvertByteToStringUsingToStringMethod.java

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

public class ConvertByteToStringUsingToStringMethod {

	public static void main(String[] args) {

		// primitive byte data-type
		byte byteValue1 = 102;

		// 1. converting byte to String 
		// by using toString() method
		String str1 = Byte.toString(byteValue1);
		System.out.println("1. Converted"
				+ " primitive byte to String value is : "
				+ str1);


		// primitive byte data-type (negative value)
		byte byteValue2 = -97;

		// 2. converting byte to String
		// by using toString() method
		String str2 = Byte.toString(byteValue2);
		System.out.println("\n2. Converted"
				+ " negative primitive byte to String value is : "
				+ str2);


		// Byte object
		Byte byteValue3 = new Byte((byte)105);

		// 3. converting Byte object to String 
		// by using toString() method
		String str3 = Byte.toString(byteValue3);
		System.out.println("\n3. Converted"
				+ " Byte object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive byte to String value is : 102

2. Converted negative primitive byte to String value is : -97

3. Converted Byte object to String value is : 105

 

Way 2: Using String.valueOf(byteVal); method

  • This method can be used to convert primitive byte data-type or Byte wrapper-type to String object

Method signature:

public static String valueOf(int i);

ConvertByteToStringUsingValueOfMethod.java

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

public class ConvertByteToStringUsingValueOfMethod {

	public static void main(String[] args) {

		// primitive byte data-type
		byte byteValue1 = 111;

		// 1. converting byte to String 
		// by using valueOf() method
		String str1 = String.valueOf(byteValue1);
		System.out.println("1. Converted"
				+ " primitive byte to String value is : "
				+ str1);


		// primitive byte data-type (negative value)
		byte byteValue2 = -87;

		// 2. converting negative byte to String 
		// by using valueOf() method
		String str2 = String.valueOf(byteValue2);
		System.out.println("\n2. Converted"
				+ " negative primitive byte to String value is : "
				+ str2);


		// Byte object
		Byte byteValue3 = new Byte((byte)15);

		// 3. converting Byte object to String 
		// by using valueOf() method
		String str3 = String.valueOf(byteValue3);
		System.out.println("\n3. Converted"
				+ " Byte object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive byte to String value is : 111

2. Converted negative primitive byte to String value is : -87

3. Converted Byte object to String value is : 15

 

Way 3: create Byte object and then convert to String using toString() method {new Byte(byteValue).toString();}

  • Here, we will create new Byte object with primitive byte value as constructor-argument
  • After creating new Byte object by passing primitive byte value, then invoke toString() method for converting primitive byte data-type to String object

Method signature:

public Byte(byte value);

public String toString();

ConvertByteToStringUsingObjectCreation.java

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

public class ConvertByteToStringUsingObjectCreation {

	public static void main(String[] args) {

		// primitive byte data-type
		byte byteValue1 = 65;

		// 1. converting byte to String 
		// by creating Byte object 
		Byte byteObj1 = new Byte(byteValue1);

		// and then invoke toString(); method
		String str1 = byteObj1.toString();
		System.out.println("1. Converted"
				+ " primitive byte to String value is : "
				+ str1);


		// primitive byte data-type (negative value)
		byte byteValue2 = -84;

		// 2. converting negative byte to String 
		// by creating Byte object
		Byte byteObj2 = new Byte(byteValue2);

		// and then invoke toString(); method
		String str2 = byteObj2.toString();
		System.out.println("\n2. Converted"
				+ " negative primitive byte to String value is : "
				+ str2);


		// Byte object
		Byte byteObj3 = new Byte((byte)26);

		// 3. converting Byte object to String 
		// by creating Byte object
		String str3 = byteObj3.toString();
		System.out.println("\n3. Converted"
				+ " Byte object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive byte to String value is : 65

2. Converted negative primitive byte to String value is : -84

3. Converted Byte object to String value is : 26

 

Way 4: adding double quotes (“”) to byte value

  • Note: for this conversion to work, double-quotes (“”) must be added at the start of the concatenation

Syntax:

String temp = “” + byteValue;

ConvertByteToStringByAddingDoubleQuotes.java

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

public class ConvertByteToStringByAddingDoubleQuotes {

	public static void main(String[] args) {

		// primitive byte data-type
		byte byteValue1 = 76;

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


		// primitive byte data-type (negative value)
		byte byteValue2 = -96;

		// 2. converting negative byte to String by adding ""
		String str2 = "" + byteValue2;
		System.out.println("\n2. Converted"
				+ " negative primitive byte to String value is : "
				+ str2);


		// Byte object
		Byte byteValue3 = new Byte((byte)56);

		// 3. converting Byte object to String by adding ""
		String str3 = "" + byteValue3;
		System.out.println("\n3. Converted"
				+ " Byte object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive byte to String value is : 76

2. Converted negative primitive byte to String value is : -96

3. Converted Byte object to String value is : 56

 

Way 5: Using append() method of StringBuffer & StringBuilder

  • This method can be used to convert primitive byte data-type to String object by appending to StringBuffer or StringBuilder object
  • And then invoking toString() method

Syntax:

StringBuffer sb = sb.append(byteValue);

String temp = sb.toString();

ConvertByteToStringByAppending.java

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

public class ConvertByteToStringByAppending {

	public static void main(String[] args) {

		// primitive byte data-type
		byte byteValue1 = 83;

		// 1. converting byte to String 
		// by using append() method
		StringBuffer sbf1 = new StringBuffer();
		String str1 = sbf1.append(byteValue1).toString();
		System.out.println("1. Converted"
				+ " primitive byte to String value is : "
				+ str1);


		// primitive byte data-type (negative value)
		byte byteValue2 = -33;

		// 2. converting negative byte to String 
		// by using append() method
		StringBuffer sbf2 = new StringBuffer();
		String str2 = sbf2.append(byteValue2).toString();
		System.out.println("\n2. Converted"
				+ " negative primitive byte to String value is : "
				+ str2);


		// Byte object
		Byte byteValue3 = new Byte((byte)93);

		// 3. converting Byte object to String 
		// by using append() method
		StringBuffer sbf3 = new StringBuffer();
		String str3 = sbf3.append(byteValue3).toString();
		System.out.println("\n3. Converted"
				+ " Byte object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive byte to String value is : 83

2. Converted negative primitive byte to String value is : -33

3. Converted Byte object to String value is : 93

 

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);

		// 1. 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.

 

References:

 

Happy Coding !!
Happy Learning !!

String to Byte[] Array conversion in Java
String to Byte conversion in Java - 3 ways