10

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!

Nick
  • 605
  • 3
  • 11
  • 27

3 Answers3

18

Only Integer has a method to convert to binary string representation check this out:

import java.io.UnsupportedEncodingException;

public class TestBin {
    public static void main(String[] args) throws UnsupportedEncodingException {
        byte[] infoBin = null;
        infoBin = "this is plain text".getBytes("UTF-8");
        for (byte b : infoBin) {
            System.out.println("c:" + (char) b + "-> "
                    + Integer.toBinaryString(b));
        }
    }
}

would print:

c:t-> 1110100
c:h-> 1101000
c:i-> 1101001
c:s-> 1110011
c: -> 100000
c:i-> 1101001
c:s-> 1110011
c: -> 100000
c:p-> 1110000
c:l-> 1101100
c:a-> 1100001
c:i-> 1101001
c:n-> 1101110
c: -> 100000
c:t-> 1110100
c:e-> 1100101
c:x-> 1111000
c:t-> 1110100

Padding:

String bin = Integer.toBinaryString(b); 
if ( bin.length() < 8 )
  bin = "0" + bin;
stacker
  • 68,052
  • 28
  • 140
  • 210
  • maybe you need some padding for your bytes – stacker Nov 13 '10 at 18:23
  • @stacker why is the space endoded on only 6 digits and not 7? (ex: c: -> 100000) – Nick Nov 17 '10 at 21:27
  • should I manualy add a 0 at the begining? (ex: (ex: c: -> 0100000) ) – Nick Nov 17 '10 at 21:31
  • @Nick that's what I meant whith my first comment, leading zeros are omitted so you need to – stacker Nov 17 '10 at 23:43
  • @Nick that's what I meant whith my first comment, leading zeros are omitted so you need to add them as you already noticed. Or you write a method which adapt the behaviour Integer.toBinaryString(). If it is not critical I would suggest to pad the strings with leading '0's. – stacker Nov 17 '10 at 23:49
  • @stacker how can I make it work for a byte that starts with "1" ? If I do it on a byte that starts with "0", I get 8 chars, as expected. If I do it on a byte that starts with "1", I get 32 chars instead. – Nick Dec 01 '10 at 03:35
  • @Nick check String.length() and use substring(0,8) (not tested) – stacker Dec 01 '10 at 07:42
  • Sorry!! If i would like get binary of "%" (it's 00100101) in this way i will receive "0" + "100101" and not "00100101". – Pippi Jan 27 '16 at 15:10
3

Arrays do not have a sensible toString override, so they use the default object notation.

Change your last line to

return Arrays.toString(infoBin);

and you'll get the expected output.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • Hi Andrzej, your answer makes a lot of sense but I get [84, 104, 105, 115, 32, 105, 115, 32, 97, 32,..] instead of (01101010...) any suggestions? thank you – Nick Nov 13 '10 at 18:16
0

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.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347