2

Possible Duplicate:
Conversion of byte[] into a String and then back to a byte[]

I have the following piece of code, I'm trying to get the test to pass, but can't seem to get my head around the various forms of encoding that go on in the java world.

import java.util.Arrays;

class Test {

static final byte[] A = { (byte)0x11, (byte)0x22, (byte)0x33, (byte)0x44, (byte)0x55, (byte)0x66, (byte)0x77, (byte)0x88, (byte)0x99, (byte)0x00, (byte)0xAA };

public static void main(String[] args) {

    String s = new String(A);

    byte[] b = s.getBytes();

    if (Arrays.equals(A,b)) { 
       System.out.println("TEST PASSED!"); 
    }
    else {
       System.out.println("TEST FAILED!");
    }

}
}

I guess my question is: What is the correct way to convert a byte array of arbitary bytes to a Java String, then later on convert that same Java String to another byte array, which will have the same length and same contents as the original byte array?

5
  • I get test passed, are you sure you're running your latest code? Commented Sep 22, 2011 at 1:18
  • What's the encoding of your original byte array? Commented Sep 22, 2011 at 1:19
  • 1
    @Mike K: ideone.com/To8IK Commented Sep 22, 2011 at 1:20
  • @MarkElliot: This is some binary data, there is no underlying 'encoding' for A. Commented Sep 22, 2011 at 1:21
  • 1
    @Xander Tulip If there is a string, there is always encoding. If you print out the bytes individually for(byte iB : b) System.out.println(Integer.toHexString(iB ));, you'll see the trouble starts at 0x88. Commented Sep 22, 2011 at 1:24

2 Answers 2

7

Try a specific encoding:

String s = new String(A, "ISO-8859-1");

byte[] b = s.getBytes("ISO-8859-1");

ideone link

3
  • 1
    that looks like it did the trick. I was one follow up question, is it correct to use a java String to pass a byte array across JNI boundaries? Commented Sep 22, 2011 at 1:33
  • 1
    @XanderTulip I'm not really familiar with JNI but there generally using string to represent byte array could be lossy. Isn't there a jbyteArray type for this? Commented Sep 22, 2011 at 1:36
  • 1
    @XanderTulip You can pass byte arrays across JNI boundaries.See java.sun.com/developer/onlineTraining/Programming/JDCBook/… Commented Sep 22, 2011 at 2:06
4

Use Base64.

Apache Commons Codec has a Base64 class which supports the following interface:

String str = Base64.encodeBase64String(bytes);
byte[] newBytes = Base64.decodeBase64(str);
2
  • 1
    This seems like a very round about way to get a very specific and simple string conversion done, futhermore it requires the use of a nonstandard java library, not sure if the OP had that in mind. Is this a joke answer? ;) Commented Feb 10, 2013 at 5:55
  • 1
    Yes, it is round-about. No, it is not a joke answer =) The OP indicated that the bytes are arbitrary. If the encoding (magic!) he chooses isn't supported by the platform or the byte array contains values that aren't supported by the encoding, an exception will be raised. Even assuming he selects the right encoding, has a regression testing suite and never intends to move platforms, he is probably using Apache Commons for something else - it's a very common library. ps. apologies for the late response Commented Apr 5, 2013 at 1:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.