0

Here is some text "Hi hello" if I convert it to bytes it will print "[B@139a55" , Without giving any string directly with this byte "[B@139a55"

can I convert a byte[] to a string in Java ??

byte[] bytes = aBuffer.getBytes(); 
System.out.println("Text [Byte Format] : " + bytes.toString());
String s = new String(bytes); 
System.out.println("Text Decryted : " + s);
Cœur
  • 37,241
  • 25
  • 195
  • 267
jerry
  • 21
  • 1
  • 3
  • 2
    Note: `String` is a sequence of `char`, not `byte`. – Andy Turner Feb 03 '17 at 10:44
  • You will find a similar question and answer on http://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte – Sylvain Leroy Feb 03 '17 at 10:45
  • 2
    `[B@139a55` is not a byte sequence. Insteads, it's simply the debug output for byte arrays. It basically means: the byte array at address 139a55. – Codo Feb 03 '17 at 10:48
  • You are not outputting it right. Show your code. – Fildor Feb 03 '17 at 10:50
  • byte[] bytes = aBuffer.getBytes(); System.out.println("Text [Byte Format] : " + bytes.toString());String s = new String(bytes); System.out.println("Text Decryted : " + s); – jerry Feb 03 '17 at 11:11
  • @Codo Reverse engineering Is that possible to get text back with that address. – jerry Feb 03 '17 at 11:16
  • @AndyTurner For getting byte format I am using a string " byte[] bytes = aBuffer.getBytes();" Here is that aBuffer is string. – jerry Feb 03 '17 at 11:20
  • @Fildor I want to output that text – jerry Feb 03 '17 at 11:22
  • 1
    @Codo note the number is a generated 31-bit hashCode not an address (which could be 64-bit) and it doesn't change as the byte[] is moved around in memory. – Peter Lawrey Feb 03 '17 at 11:26
  • 2
    @jerry the number is a randomly generated hashCode which has no information you can use to recreate the original contents of the `byte[]`. – Peter Lawrey Feb 03 '17 at 11:27
  • 1
    there is no such thing as `pointer` in Java, so there is no such thing as 'address' as well. What you see in the output is a default implementation of `toString()` method in `java.lang.Object` – Vadim Feb 03 '17 at 11:29
  • @PeterLawrey the string is assigned to that byte ,once we give that byte[] the string "hi hello" is printed ,likewise is there possible to give just the byte[] to get text.how we recreate the original content of that byte[]?? – jerry Feb 03 '17 at 12:02
  • @jerry " the string is assigned to that byte" - no it is not. You don't print what you think you are printing. – Fildor Feb 03 '17 at 12:04

2 Answers2

2

[72, 105, 32, 104, 101, 108, 108, 111] - Is it what you'd like to see out of that string Hi Hello ?

Then do that:

    String str = "Hi hello";

    byte[] bytes = str.getBytes();

    // convert
    Byte[] bytesWrap = new Byte[bytes.length];
    for (int i = 0; i < bytes.length; i++) {
        bytesWrap[i] = bytes[i];
    }
    String bytesStr = Arrays.asList(bytesWrap).toString();
    //print result
    System.out.println("bytes in \"" + str + "\":" + bytesStr);

UPDATED: Explanation: default implementation of java.util.List.toString() produces string like [ element.toString(),element.toString() ...] Then there is a java.utils.Arrays utilitiy which can convert array to list, but... elements must be Objects, not primitives.

So, steps are 1. convert from byte[] to corresponding Object wrapper Byte[] 2. convert Byte[] to List 3. Use List.toString()

Of course it is just a demonstration how to use List to simply print Array which is good for objects. For that particular case with primitives there is more simple code.

    String str = "Hi hello";

    StringBuilder bytesStrBuilder =new StringBuilder("[ ");
    for (byte bt:str.getBytes()) {
        bytesStrBuilder.append(bt).append(" ");
    }       
    String bytesStr = bytesStrBuilder.append("]").toString();

    System.out.println("bytes in \"" + str + "\":" + bytesStr);
Vadim
  • 4,027
  • 2
  • 10
  • 26
  • You should explain the subtlety of this wrap. And I don't see the point of using `asList` a second time – AxelH Feb 03 '17 at 11:26
  • @AxelH - you are right - my bad. :-) – Vadim Feb 03 '17 at 11:51
  • I like the idea, but you seems to have forget a simpler solution, check the answer I have write ;) – AxelH Feb 03 '17 at 11:58
  • @AxelH - sure `Arrays.toString()` is better. It was early morning :-) and it was new to me that Arrays.asList() does not take `byte[]`, or any other primitive arrays. Since usually I have no primitive arrays I use asList() often enough. – Vadim Feb 03 '17 at 13:41
2

I am not really sure of what you asked, but to convert a Byte[] into the Sting, simply use the String(byte[]) constructor.

String s = new String("foo bar".getBytes());`

If you want to print each bytes of a String, Vadim solution is correct but there is a much shorter version. I used his output line to keep the format

byte[] bytes = "Hi hello".getBytes();
System.out.println("bytes in \"" + new String(bytes) + "\":" + Arrays.toString(bytes));

The idea is the same but the Arrays.toString method will do it for you since it accept a byte[], it will iterate through it and generate a String using a StringBuilder.

AxelH
  • 14,325
  • 2
  • 25
  • 55