5

I tried to convert byte[] to string as follows:

Map<String, String> biomap = new HashMap<String, String>();
biomap.put("L1", new String(Lf1, "ISO-8859-1"));

where Lf1 is byte[] array and then i convert this string to byte[]: problem is, when i convert byte array to string it comes like:

FMR  F P�d@� �0d@r (@� ......... etc

and

String SF1 = biomap.get("L1");
byte[] storedL1 = SF1.getBytes("ISO-8859-1")

and when i convert back it to byte array and compare both arrays, it return false. I mean Data Changed.

i want same byte[] data as it was when i encoded to string and decodec to byte[]

4
  • 3
    Write out 100 times. 'String is not a container for binary data'. Commented Jun 17, 2017 at 9:30
  • How do you compare arrays, by the way? Don't you use oldArray.equals(newArray)? (spoiler: this is not a correct way to compare arrays) Commented Jun 17, 2017 at 10:07
  • @EJP i have to store binary data into varchar2 type column that's why i used this approach but if you have better one. please suggest. i will appreciate that. Commented Jun 17, 2017 at 10:12
  • @RomanPuchkovskiy actually i am comparing two fingerprint data and using Match(byte[] a, byte[] b) function provided by manufacturer's API. Commented Jun 17, 2017 at 10:13

2 Answers 2

10

First: ISO-8859-1 does not cause any data loss if an arbitrary byte array is converted to string using this encoding. Consider the following program:

public class BytesToString {
    public static void main(String[] args) throws Exception {
        // array that will contain all the possible byte values
        byte[] bytes = new byte[256];
        for (int i = 0; i < 256; i++) {
            bytes[i] = (byte) (i + Byte.MIN_VALUE);
        }

        // converting to string and back to bytes
        String str = new String(bytes, "ISO-8859-1");
        byte[] newBytes = str.getBytes("ISO-8859-1");

        if (newBytes.length != 256) {
            throw new IllegalStateException("Wrong length");
        }
        boolean mismatchFound = false;
        for (int i = 0; i < 256; i++) {
            if (newBytes[i] != bytes[i]) {
                System.out.println("Mismatch: " + bytes[i] + "->" + newBytes[i]);
                mismatchFound = true;
            }
        }
        System.out.println("Whether a mismatch was found: " + mismatchFound);
    }
}

It builds an array of bytes with all possible byte values, then it converts it to String using ISO-8859-1 and then back to bytes using the same encoding.

This program outputs Whether a mismatch was found: false, so bytes->String->bytes conversion via ISO-8859-1 yields the same data as it was in the beginning.

But, as it was pointed out in the comments, String is not a good container for binary data. Specifically, such a string will almost surely contain unprintable characters, so if you print it or try to pass it via HTML or some other means, you will get some problems (data loss, for example).

If you really need to convert byte array to a string (and use it opaquely), use base64 encoding:

String stringRepresentation = Base64.getEncoder().encodeToString(bytes);
byte[] decodedBytes = Base64.getDecoder().decode(stringRepresentation);

It takes more space, but the resulting string is safe in regard to printing.

Sign up to request clarification or add additional context in comments.

4 Comments

that's why i was using ISO-8859-1 encoding but unfortunately output was not what i desired. anyway now it is working. thanks for quick reply. +1
Is there anyway to short the length of String which is generated from byte[]?
@VishalSenjaliya you could you base64 or another baseXX variant that suits your data
@RomanPuchkovskiy base64 would increase the length of his data by factor of roughly 4/3, in terms of number of characters, compared to what he's using above. Since Java uses UTF-16 as the string encoding it may be possible to encode ~1.5 bytes per character or so with some whackadoodle encoding, but in terms of memory footprint you will never have a String encoding of a byte sequence that takes less memory than the byte sequence itself.
2

There are special encodings like base64 for encoding binary data for text only systems.

Converting a byte[] to String is only guaranteed to work, if the byte[] contains a valid sequence of bytes according to the chosen encoding. Unknown byte sequences might be replaced with the unicode replacement character (as shown in your example).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.