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?

6
  • 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? Commented Apr 10, 2016 at 19:17
  • 1
    Is this what you are looking for? Commented Apr 10, 2016 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. Commented Apr 10, 2016 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? Commented Apr 10, 2016 at 19:27
  • Sorry guys, I forgotten to specify that I need the representation in 7-bit Commented Apr 10, 2016 at 20:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 ?
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.
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());
}

2 Comments

If for some reason, you are going to include things that require 8-bits or 7-bits or something else, I think it is not hard to find the 8 that you will have to change. :)
Thank's for your help buddy :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.