Java Convert Byte Array Char Array

Java Convert Byte Array Char Array


Java Convert Byte Array Char Array

Summary: Explained in this tutorial "Java Convert Byte Array Char Array", converting in between byte array and char array in simple Java code.

Sometimes it is very much required to convert byte array to char array and vice versa in coding. Following program illustrates the conversion of byte array to char array and char array to byte array and also to convert single byte to char and single char to byte.


Java Convert Byte Array Char Array
Output screen of Java Convert Byte Array Char Array

Here, we take the help of String constructors and methods. The String constructors are overloaded that takes byte and char arrays as parameters. toCharArray() method of String returns a char array and getBytes() method returns a byte array. In between the conversions, String class is used.

The other methods of String class useful in these conversions are getChars() and overloaded copyValueOf().

Here, enhanced for loop is used. This loop comes into existence with JDK 1.5.

Three questions for you.

1. Can you code like this?

System.out.println(pow(2,4)); // Math class is not written

instead of traditional

System.out.println(Math.pow(2,4)); // Math is written

Ans: Yes, it is possible from JDK1.5 version with a new feature known as static imports.

2. Can you use C-lang printf() in Java with all %d and %s etc options.?
Ans: Yes, it is possible from JDK1.5 version.

3. Can you use C-lang enums in Java?
Ans: Yes, it is possible from JDK1.5 version.

3 thoughts on “Java Convert Byte Array Char Array

Leave a Reply

Your email address will not be published. Required fields are marked *