I have a code to convert anyway English alphabetical value to binary code (a --> 01100001). The code is working fine with one exception. when the result appears after conversion, it is missing a "0" in the front. Ex: a --> 1100001. It does this regardless of either capital of lowercase letter.
Why is it doing that? All I need is 0 in the front. Reference: http://www.convertbinary.com/alphabet/
Note: the string value (user first name) is input by the end user, not in the code it self as some threads on here about conversion shows.
public class ConversionController {
/**
* The getBinaryName is used to convert string to binary form
*
* @param name
* to be converted
* @return converted binary name
*/
public String getBinaryName(String name) {
if (name == null || name.isEmpty()) {
return "";
}
String result = "";
char[] messChar = name.toCharArray();
for (int i = 0; i < messChar.length; i++) {
result += Integer.toBinaryString(messChar[i]) + " ";
}
return result;
}
}
SOLUTION: added "0" in front of the conversion result. result+= "0" +Integer.toBinaryString(messChar[i]) + " ";