I want to get binary (011001..) from a String but instead i get [B@addbf1 , there must be an easy transformation to do this but I don't see it.
public static String toBin(String info){
byte[] infoBin = null;
try {
infoBin = info.getBytes( "UTF-8" );
System.out.println("infoBin: "+infoBin);
}
catch (Exception e){
System.out.println(e.toString());
}
return infoBin.toString();
}
Here i get infoBin: [B@addbf1
and I would like infoBin: 01001...
Any help would be appreciated, thanks!
Only Integer has a method to convert to binary string representation check this out:
would print:
Padding:
Arrays do not have a sensible
toString
override, so they use the default object notation.Change your last line to
and you'll get the expected output.
When you try to use
+
with an object in a string context the java compiler silently inserts a call to the toString() method.In other words your statements look like
System.out.println("infobin: " + infoBin.toString())
which in this case is the one inherited from Object.
You will need to use a for-loop to pick out each byte from the byte array.