How to convert Java String to byte[] Array

Post Banner

In order to convert a String to byte[] array in Java make use of the .getBytes() method from the String class,

public class JavaStringToByteArray {

    public static void main(String[] args) {

        String string = "Hello World!";
        byte[] byteArr = string.getBytes();

        for(int i = 1;i <byteArr.length; i++) {
            System.out.print(byteArr[i]+" ");

101 108 108 111 32 87 111 114 108 100 33 %

To convert byte[] array to String:

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap