5 Different Ways to Print Byte Array in Java

Print Byte Array in Java | In this post we will discuss how to print byte array as string in Java programming language. The byte array will contain N number of byte values ranging between -128 and 127. 

To print byte arrays, we can use loops (for loop, for-each loop, while loop, do-while loop) or in-built methods like Arrays.toString(). Here we will see the following ways to print byte array in Java:- 

  1. Using the Arrays.toString() method
  2. Using the Arrays.deepToString() method
  3. Using for-each loop
  4. Using for loop
  5. Using a while loop

How To Print Byte Array As String In Java using Arrays.toString()

Java has provided an Arrays class in java.util package which contains array manipulation methods for the task like searching, sorting, copying, and e.t.c. It also contains the toString() method which converts an array to a string. 

Following is the method syntax of the Arrays.toString() method which can be used to convert byte array to string in Java:- public static String toString(byte[] a)

Java Program to Print Byte Array As String using Arrays.toString()

import java.util.Arrays;

public class Main {
    public static void main(String args[]) {
        byte[] byteArray = { 10, 20, 90, 5, 127, -50 };
        System.out.println(Arrays.toString(byteArray));
    }
}

Output:-

[10, 20, 90, 5, 127, -50]

import java.util.Arrays;

public class Main {
    public static void main(String args[]) {
        byte[] byteArray1 = 
                    new byte[] { (byte) 500, (byte) 400, (byte) 300, 100 };
        System.out.println(Arrays.toString(byteArray1));

        System.out.println(Arrays.toString(
              new byte[] { (byte) 500, (byte) 400, (byte) 300, 100 }));

        byte[] byteArray2 = new byte[3];
        byteArray2[0] = 10;
        byteArray2[1] = (byte) 650;
        byteArray2[2] = (byte) -800;
        System.out.println(Arrays.toString(byteArray2));
    }
}

Output:-

[-12, -112, 44, 100]
[-12, -112, 44, 100]
[10, -118, -32]

The Arrays.toString() can print only a one-dimensional byte array as a string. But if we have a multi-dimensional byte array then we can take the help of another method provided by the Java Arrays class:- deepToString().

Following is the syntax of the deepToString() method:- public static String deepToString(Object[] a)

import java.util.Arrays;

public class Main {
    public static void main(String args[]) {
        byte[][] byteArray = { { 10, 20, 90 }, { 5, 127, (byte) 350 } };
        System.out.println(Arrays.deepToString(byteArray));
    }
}

Output:-

[[10, 20, 90], [5, 127, 94]]

We can also use a for-each loop to print byte array in Java. The for-each loop will iterate the byte array, fetch each and every element of the array, and then we can display them.

public class Main {
    public static void main(String args[]) {
        byte[] byteArray = { 10, 20, 90, 5, 127, (byte) 350 };
        System.out.println("Byte array: ");
        for (byte val : byteArray) {
            System.out.print(val + " ");
        }
    }
}

Output:-

Byte array:
10 20 90 5 127 94

For index-based iteration of the array, we can use for loop. The array elements

public class Main {
    public static void main(String args[]) {
        byte[] byteArray = { 10, 20, 90, 5, 127, (byte) 350 };
        System.out.println("Byte array: ");
        for (int i = 0; i < byteArray.length; i++) {
            System.out.print(byteArray[i] + " ");
        }
    }
}

Output:-

Byte array:
10 20 90 5 127 94

The Arrays.toString() method and Arrays.deepToString() directly converts an array to a string. And the for-each loop iterates the array. But before printing, if we want to modify the array then we can’t use any of the above three methods. In that case, we can use for loop, while loop, or do-while loop.

public class Main {
    public static void main(String args[]) {
        byte[] byteArray = { 10, 20, 90, 5, 127, (byte) 350 };
        System.out.println("Byte array: ");
        byte sum = 0;
        for (int i = 0; i < byteArray.length; i++) {
            // Cumulative Sum
            sum += byteArray[i];
            byteArray[i] = sum;
            System.out.print(byteArray[i] + " ");
        }
    }
}

Output:-

Byte array:
10 30 120 125 -4 90

public class Main {
    public static void main(String args[]) {
        byte[] byteArray = { 10, 20, 90, 5, 127, (byte) 350 };
        System.out.println("Byte array: ");
        byte i = 0;
        while (i <= byteArray.length) {
            System.out.print(byteArray[i] + " ");
            i++;
        }
    }
}

Output:-

Byte array:
10 20 90 5 127 94

In the while loop first, we declare & initialize then variable outside the loop. Later used within the loop, and getting updated after that.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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