0

I wrote a code writing 8 different values of sine in byte form (using .getbytes) into a text file.

After I run it a file is created containing the following: [B@5f18cd5 [B@1c515979 [B@20c92575 [B@75ba3523 [B@f4c7f77 [B@67446579 [B@3b621fe6 [B@271c537f

So far so good...

I'd now like to inverse this whole process in another Java project. For this I need to know how to turn for example [B@1c515979 back to it's initial value which is 0.7071.

I tried to use

System.out.println("Text [Byte Format] : " + bytes.toString());

with which I hoped to convert the byte code back to string. The problem though, is that since I'm reading from a text file I guess the read data is string anyway, so actually I'm just turning strings into strings.

This is my status quo... Anyone got an idea?

Thanks for listening.

3
  • why don\t you store the values as a text in the first place? Commented Sep 16, 2016 at 7:46
  • Do you call the getBytes() on a String? Then you should use new String(bytes); instead of bytes.toString();. Commented Sep 16, 2016 at 7:50
  • "So far so good" ... No, not good. You're writing the data in the wrong way. The [B@... strings you see are not representations of byte arrays. You get these because arrays don't have a toString method. Commented Sep 16, 2016 at 7:53

3 Answers 3

2

[B@5f18cd5 is not a byte, it's a string representation of a reference. You can't turn them back into anything.

public class Ref {
    public static void main(final String[] args) {
        System.out.println(new Ref());
    }
}
//Outputs: Ref@19e0bfd

foo@address is a reference. You're not actually writing values into the file but references as String. B[ means that you're writing byte[] into a file but not the actual bytes in that array.

Update

You're probably looking for something like this:

public static void main(final String[] args) throws FileNotFoundException,
        IOException {
    final File f = new File("/tmp/output");

    final ObjectOutputStream oos = new ObjectOutputStream(
            new FileOutputStream(f));

    for (int i = 0; i < 8; i++) {
        final double d = Math.sin(i);
        System.out.println(d);
        oos.writeDouble(d);
    }

    oos.flush();
    oos.close();

    System.out.println("---");

    final ObjectInputStream ois = new ObjectInputStream(
            new FileInputStream(f));

    for (int i = 0; i < 8; i++)
        System.out.println(ois.readDouble());

    ois.close();

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

Comments

1

You can't. You've already lost all the important data. Calling toString() on a byte[] doesn't give you anything useful, because arrays in Java don't override toString()... so you're getting the implementation from Object, which just indicates the type and the hash code (which is effectively the identity hash code, for arrays). If you modify the content of the byte array and call toString() on it, you'll get the same value.

Instead, you need to change how you're saving the data. If you can avoid needing a text file at all, that would be ideal... but if you do need a text file, the simplest option is probably to convert the binary data to base64, e.g. using java.util.Base64:

String text = Base64.getEncoder().encodeToString(bytes);
// Write out text...

...

// String text = // read from a file
byte[] bytes = Base64.getDecoder().decode(text);

Comments

0

The strings that you show ([B@5f18cd5 etc) are not encodings of the byte values. Rather they are the output of the default toString() method of java.lang.Object: 'class name' + "@" 'hash code in hex'.

You can't convert that back into the contents of the bytes.

You need to change the way in which you write the bytes. Try DataOutputStream to write each byte array.

For each array, write the length of the array using DataOutputStream.writeInt() and then the bytes themselves. To read them back, use DataInputStream, first read the length, then construct a byte array of that length, and then read the data back into the array.

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.