Thursday, February 9, 2012

How to convert String to byte array and vice-versa in android?

// This method converts an byte array to a String object.

    public void convertByteArrayToString() {

        byte[] byteArray = new byte[] {877987464646};

        String value = new String(byteArray);

        System.out.println(value);
    }



     // This method converts a String to an array of bytes
    public void convertStringToByteArray() {

    String stringToConvert = "This String is 76 characters long and will be converted to an array of bytes";

        byte[] theByteArray = stringToConvert.getBytes();

        System.out.println(theByteArray.length);

    }

No comments:

Post a Comment