1

I am currently working on a project that would convert ASCii string text into Binary digits, but I've come upon several issues. First off, I would like to know exactly how can I take single digit from a String and print out it's Binary offspring, secondly What would be the best method of applying this? Thanks

TechTitan
  • 11
  • 1
  • 1
  • 3
  • see here : http://stackoverflow.com/questions/917163/convert-a-string-like-testing123-to-binary-in-java – Pat B Nov 08 '13 at 20:27

1 Answers1

9
public static String AsciiToBinary(String asciiString){  

          byte[] bytes = asciiString.getBytes();  
          StringBuilder binary = new StringBuilder();  
          for (byte b : bytes)  
          {  
             int val = b;  
             for (int i = 0; i < 8; i++)  
             {  
                binary.append((val & 128) == 0 ? 0 : 1);  
                val <<= 1;  
             }  
            // binary.append(' ');  
          }  
          return binary.toString();  
    }  
constantlearner
  • 5,157
  • 7
  • 42
  • 64