-4

I have a problem converting strings to bytes. My problem is this:

  String hello = "hello";
    byte[] bytes = hello.getBytes();
    String byteString = bytes.toString();
   

Now I want to convert this string to byte[]. and finally get "hello"

Thank you for your help

hani
  • 49
  • 1
  • 7
  • You already have a `byte[]` as in `bytes`. You can just use that. – Hanchen Jiang Dec 26 '21 at 03:35
  • 4
    That string consists of the type name (`[B`), and its system identity hashcode. It is __impossible__ to go from that string back to the original. – rzwitserloot Dec 26 '21 at 03:39
  • Thank you very much, but I want to get to the bytes from the byteString – hani Dec 26 '21 at 03:39
  • 1
    The problem is that calling `toString` on a `byte[]` returns a `String` that does **not** represent the bytes in the array. Your `byteString` will look similar to `"[B@2ff4acd0"`, which is a combination of the type and the hashcode. You cannot convert that back to a `byte[]`. – f1sh Dec 26 '21 at 03:47
  • hi "f1sh" I have to send a string of bytes and get the byte on the other hand what is the best thing to do – hani Dec 26 '21 at 03:51
  • What do you need to send it as? A `String` or `byte[]`? Converting the `byte[]` to a `String` can be done using `new String(yourByteArray);` – f1sh Dec 26 '21 at 03:53
  • I want to send the bytes.toString() and receive the byte string on the other side. as I say that – hani Dec 26 '21 at 04:03
  • 1
    Duplicate: [*Why does the toString method in java not seem to work for an array*](https://stackoverflow.com/q/7060016/642706) – Basil Bourque Dec 26 '21 at 06:34
  • @hani `toString()` called on array returns *type Information* like `[B` and *hash code* (in its hexadecimal form). Problem is that `hash` is NOT [injective function](https://en.wikipedia.org/wiki/Injective_function) so for two different inputs it can return same result. For instance `"Aa".hashCode()` = `"BB".hashCode()` = `2260`. So based ONLY on `2260` can you determine on what String `hashCode() method was called (`"Aa"` or `"BB"` or maybe something else)? **No, you can't**. Same thing happens with `array.toString()` (which internally uses `hashCode()`). – Pshemo Dec 26 '21 at 10:26

2 Answers2

3

When you need byte[] to String, use new String(yourArray)

When you need String to byte[], use yourString.getBytes()

        String hello = "hello";
        byte[] bytes = hello.getBytes();
        String byteString = new String(bytes);
        System.out.println("byteString: " + byteString); // byteString: hello

        byte[] newArray = byteString.getBytes();
        String otherString = new String(newArray);
        System.out.println("otherString: " + otherString); // otherString: hello

The "toString()" of byte array is the "toString()" inheritance from Object.

The method is:

    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

So, it is the reason for... if you call: yourArray.toString() returns something like [B@dcf3e99

leandro.dev
  • 345
  • 2
  • 8
  • Thank you, Mr. leandro.dev. I know this method can be used, but I want to send the bytes.toString() and receive the byte string on the other side. – hani Dec 26 '21 at 04:01
  • @hani everyone here has explained to you what can be done and what cant. – f1sh Dec 26 '21 at 13:25
0

I got the answers to my questions. Thank you all for the tips and even those who gave negative feedback to my questions.

The best way to send a string in bytes and get it in bytes is to encode to base64 and get the decode on the other side.

String hello = "hello";
byte[] bytes = hello.getBytes();
String encode = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(bytes );
byte[] decode = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.decode(encode);
System.out.println(new String(decode));

 

I solved my problem this way, I hope it will be useful for someone who has encountered this problem. Thanks everyone

hani
  • 49
  • 1
  • 7