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 2.0k times
1 Answers
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