0
How to make string to binary converter usi g java?
Please help me guys..!! I need it for my school project.! I already Learned about ASCII but only Char is difficult to understand for me..
5 Antworten
+ 2
All chars have integer codes in Unicode. So you can get these codes and convert them to binary. Like this :
String name = "Kim";
char[] array = name.toCharArray();
StringBuilder sb = new StringBuilder();
for(int i= 0; i< array.length; i++) {
String result = Integer.toBinaryString((int) array[i]);
String formattedResult = String.format("%8s", result).replaceAll(" ", "0"); sb.append(formattedResult);
sb.append(" ");
}
System.out.println(sb.toString());
So basically we want to divide a string to separate characters. Then we take these characters, receive their unicode code by casting them to integer, then we transform this integer to binary string. String.format is just to properly represent binary values. Use %8s if you are using latin characters or %16s for every other characters, like petroglyphs or something.
+ 1
May you mean printing charecter as iI value instead of charecters....!
Kim => 75 105 109
Edit : Kim L. Layao
To binary, just convert those values to binary..
The method is already given below by @Aleksandrs Kalinins
Integer.toBinaryString(int); this can directly to binary..
0
I mean when someone input their name, it converts to binary, so it goes like every character have assigned binaries
0
Numbers actually? I want some idea because I guess I can use it for marketing data