1

hence i have the bytes value of a String. For example via:

String str="Test String";
System.out.println(str.getBytes());

which gives me:

[B@1339a0dc

can i use this for initializing a bytes array such as:

byte[] bytes=new bytes("[B@1339a0dc");

or something?

0

2 Answers 2

2

getBytes returns a byte array. So you can do:

byte[] bytes = str.getBytes(); 

directly.

[B@1339a0dc is just an object reference represented as a String, it is not the actual byte array

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

Comments

1

No. That's the class name ([B is byte[]) and the hashcode (1339a0dc is the hash code in hexadecimal). Hashes cannot be reversed since they're not bijective.

Why it prints this? Because you're using an implicit toString(). This:

System.out.println(str.getBytes());

gets translated by the compiler as this:

System.out.println(str.getBytes().toString());

because System.out.println() takes a String as argument, so an implicit conversion is made here.

So you're using the default Object#toString() implementation, which works as I explained before (more details in the documentation)

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.