-2

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]) + " ";

Rman031
  • 17
  • 1
  • 3
  • 2
    It seems to me that the first thing you need to consider is what encoding you want to use. What would you want the output to be for for example? – Jon Skeet May 25 '17 at 12:54
  • Your problem is that you need padding. See this question https://stackoverflow.com/questions/4421400/how-to-get-0-padded-binary-representation-of-an-integer-in-java – Thijs Steel May 25 '17 at 12:55
  • Jon: probably :-) – GhostCat May 25 '17 at 12:55
  • Why do *you* think your code should add a 0 at the front? Does the javadoc of Integer.toBinaryString say that it does that? When I read the javadoc, I see *with no extra leading 0s*. So why are you expecting a leading zero? – JB Nizet May 25 '17 at 12:56
  • There is no need of leading zeros that is why `Integer.toBinaryString()` returns a String without leading zeros. So you will always get a binary string starting with `1`. I think you here need a `Binary String` of fixed length `8`. You can do here padding which fits the ouput in `8` length string with leading zeros as pad. But you require to do this for alphabets only as you have mentioned and all Unicode's of Alphabets use only 7 bits so you can simply add "0" before every output. – Sanket Makani May 25 '17 at 13:01
  • @SanketMakani Thanks, that's exactly what I did. I noticed your last name, curious if you're Ismaili. – Rman031 May 25 '17 at 13:59

1 Answers1

0

How about

    public String getBinaryName(String name) {
    String initial;
    int size = 8;

    if (name == null || name.isEmpty()) {
        return "";
    }

    String result = "";
    char[] messChar = name.toCharArray();

    for (int i = 0; i < messChar.length; i++) {
        initial = Integer.toBinaryString(messChar[i]);
        int times = size - initial.length();
        for(int x = 0; x < times; x++) {
            result += "0";
        }
        result += initial;
    }

    return result;
}

I just put the local variable size to 8 as a convenience. But it could be set to what you want and perhaps through an argument in the method. size equals how long you want it to be. 8 meaning 8-bits = 1 byte.

As stated by JB Nizet Integer.toBinaryString doesnt add leading 0's. It truncates it to the most significant bit. 0000 1001 becomes 1001, 0001 0001 becomes 10001 etc.

size - initial.length() checks the difference between this truncated length and the length that you want with leading zero's. That difference is then used with the variable named times to make an internal loop add zero's that many times before appending the truncated part.

brat
  • 349
  • 3
  • 11