2

I want to convert every character of a String to a new binary String. Here is what I do :

public static void main(String args[]) {
    String MESSAGE = "%";
    String binaryResult = "";
    for (char c : MESSAGE.toCharArray()){
        binaryResult += Integer.toBinaryString( (int) c);
    }
    System.err.println(binaryResult);
}

For exemple with the input : "%", I get the following output : "100101" My problem is that the leading "0" is deleted ... I want to have : "0100101". Does anyone have ideas?

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
Astrus
  • 173
  • 14
  • 1
    Please be more clear - why do you need to have a leading 0? In binary, 01001 is equivalent to 1001. How should your program be determining when do add 0s? Should it always be 7 digits? – nhouser9 Apr 10 '16 at 19:17
  • 1
    Is [this](http://stackoverflow.com/q/4421400/335858) what you are looking for? – Sergey Kalinichenko Apr 10 '16 at 19:19
  • Actually no because I dont want to specify the number of digit. I just want to have the leading 0 if there is one. – Astrus Apr 10 '16 at 19:24
  • You need to be more clear. Give a sample input and output. Describe what you need. A binary number does not have a leading number by default - that would be like if we tried to parse an integer and we made it 00034 instead of 34. In what cases do you need to pad your result with 0s? – nhouser9 Apr 10 '16 at 19:27
  • Sorry guys, I forgotten to specify that I need the representation in 7-bit – Astrus Apr 10 '16 at 20:05
  • codingame is popular :-) – Filosssof Apr 09 '17 at 18:38

2 Answers2

1

What you're really saying is "How can I pad my binary string representation of a character to 7 digits"?

Replace this line:

binaryResult += Integer.toBinaryString( (int) c);

With these:

String binString = Integer.toBinaryString( (int) c );
binaryResult += ("0000000" + binString).substring(binString.length());

This presumes that you only have 7-bit characters... if you need more, then add 0's to the "00000" string to match the length of string (with padded 0s) you want.

billjamesdev
  • 14,133
  • 6
  • 48
  • 72
  • Yes it was my goal ! I didn't make myself clear ... But in the case of the character "%" your binString var will have a length of 6 not 7 no ? – Astrus Apr 10 '16 at 20:07
  • What I meant was, a MAX of 7 bits. Try out the code and see if it works for you. That substring call says to START at char 6 in this case... which results in 7 characters in the string. – billjamesdev Apr 10 '16 at 20:51
1

I would suggest a couple of changes to your existing code. Since, you are concatenating to an string, inside a loop, this would cause the creation of a bunch of new string objects since they are immutable. The problem may be solved by use of a StringBuilder.

public static void main(String args[]) {
    String MESSAGE = "%";
    StringBuilder binaryResult = new StringBuilder();
    for (char c : MESSAGE.toCharArray()) {
        StringBuilder curValue = new StringBuilder(Integer.toBinaryString((int)c));

        // calculate padding 0 bits to fill to 8 bits
        int paddingLength = 8 - curValue.length();
        char[] paddingArr = new char[paddingLength];
        Arrays.fill(paddingArr, '0');

        // insert padding bytes to the front
        curValue.insert(0, paddingArr);

        // add to stringbuilder for `MESSAGE`
        binaryResult.append(curValue);
    }
    System.err.println(binaryResult.toString());
}
Debosmit Ray
  • 4,731
  • 2
  • 21
  • 41