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.

public class ByteArrayCharArray
{
  public static void main(String args[])
  {
                                               // converting byte array to char array
    byte b1[] = { 65, 66, 67, 68, 69, 70 };
                                               // java array to string
    String s1 = new String(b1);
    System.out.println("byte array as string: " + s1);

    char c1[] = s1.toCharArray();
    System.out.print("Chars of byte array: ");                
    for(char ch : c1)
    {
       System.out.print(ch + ", ");
    }
                                               // converting char array to byte array
    char c2[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    String s2 = new String(c2);
    System.out.println("\n\nchar array as string: " + s2);  
                                               // java string to byte array      
    byte b2[] = s2.getBytes();
    System.out.print("bytes of char array: ");                
    for(byte b : b2)
    {
      System.out.print(b + ", ");
    }    	                               // converting single byte to char
    byte b3 = 65;
    char c3 = (char) b3;
    System.out.println("\n\nbyte value is " + b3 + " and corresponding char value is " + c3);
              	                               // converting single char to byte
    char c4 = 'a';
    byte b4 = (byte) c4;
    System.out.println("char value is " + c4 + " and corresponding byte value is " + b4);
  }
}


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 Comment

Your email address will not be published.