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
Asked
Active
Viewed 1.9k times
1
-
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 Answers
8
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,077
- 7
- 41
- 62