How to make string to binary converter usi g java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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..

8th Sep 2020, 3:34 PM
Kim Layao
Kim Layao - avatar
5 Answers
+ 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.
8th Sep 2020, 9:00 PM
Aleksandrs
Aleksandrs - avatar
+ 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..
8th Sep 2020, 3:58 PM
Jayakrishna 🇮🇳
0
I mean when someone input their name, it converts to binary, so it goes like every character have assigned binaries
8th Sep 2020, 3:40 PM
Kim Layao
Kim Layao - avatar
0
Numbers actually? I want some idea because I guess I can use it for marketing data
8th Sep 2020, 3:46 PM
Kim Layao
Kim Layao - avatar