4

Possible Duplicate:
Java Byte Array to String to Byte Array

I have a method called READ() that accept a String parameter. This string is already have been converted into bytes. All I want is to convert into a readable string.

public static String READ(final String data) throws UnsupportedEncodingException{
   char[] temp = data.toCharArray();
   byte[] bytes = new byte[temp.length];
   int i = 0;
        
   for(char c : temp){
      bytes[i++] = (byte)c;
   }
        
   return new String(bytes, "UTF-8");
}

public static String SEND(String data) throws UnsupportedEncodingException{
   return data.getBytes()+"";
}

Testing:

String msg = "testing !";
String msgBytes = null;
   try {
        msgBytes = SEND(msg);
   } catch (UnsupportedEncodingException e2) {
        e2.printStackTrace();
   }
        
   System.out.println( "SEND: " + msgBytes);
   try {
         System.out.println("RECEIVE: " + READ(msgBytes));
   } catch (UnsupportedEncodingException e2) {
            e2.printStackTrace();
   }

And the OUTPUT IS:

SEND: [B@452467ec

RECEIVE: [B@452467ec

Community
  • 1
  • 1
Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137

2 Answers2

2

String has a constructor that takes byte[] as an argument.

String(byte[] bytes) -- Constructs a new String by decoding the specified array of bytes using the platform's default charset.

String(byte[] bytes, Charset charset) -- Constructs a new String by decoding the specified array of bytes using the specified charset.

So print it like this:

System.out.println(new String(msgBytes, "UTF-8"));

What you currently see is a default Object.toString() which prints a memory reference to the byte array (all arrays extend from Object).

Mirko Adari
  • 5,083
  • 1
  • 15
  • 23
  • I know, but the problem is READ() accepts a String like this: "[B@452467ec", and this isn't a byte array, it's a string. All I want is to convert this into a readable String, I want the output to be "testing !" – Zbarcea Christian Jan 22 '13 at 19:59
  • 1
    If READ received that stringified reference, there's nothing sane it can to to turn it back into a readable string. All that string says is "Byte Array at address 0x452467ec", you have lost all other information at that point. You need to fix whatever sent that string to READ. – Chuck Adams Jan 22 '13 at 19:59
  • I made a chat application with eclipse RCP. I'm sending a chunk of bytes (with SEND() method, which converts a string into a byte array) to the client/server. When I'm reading on the client/server side the stream I'm reading "[B@452467ec". This is what I want to convert back to string. – Zbarcea Christian Jan 22 '13 at 20:02
  • 1
    [B@452467ec is a byte array, when you print it out, it invokes Objec.toString() that prints out a memory reference to the byte array. Please just try out my example. – Mirko Adari Jan 22 '13 at 20:03
  • 1
    As I have just said, it is impossible to turn an array's default string representation (which is just Object.toString()) back into a String. The client is misbehaving and *must* be fixed. Put the call to "new String(array,charset)" on the *client* side (don't ever use the single-arg form of new String) – Chuck Adams Jan 22 '13 at 20:04
  • Now I get it... "[B@452467ec" it's just a reference. I can't read on the server side, because it's doesn't exists! – Zbarcea Christian Jan 22 '13 at 20:06
1

You print a byte[] which is an Object, so it just prints the reference into memory of that Object because that's what the default implementation of toString() does.

You should print the String directly. Don't print its byte[] representation.

What you do in your code is to transform a String into byte[] and then back to String in the wrong way.

Dan D.
  • 32,246
  • 5
  • 63
  • 79