1

I tried to encrypt with the OTP (One Time Pad). I have coded a "test"-code to see if I make everything right, or not.

    final String message = "Hello";
    char character;
    String binary;

    for (int i = 0; i < message.length(); i++) {
        character = message.charAt(i);
        binary = Integer.toBinaryString(character);
        System.out.println(character + ": " + binary);
    }

So, there are the following:

H: 1001000
e: 1100101
l: 1101100
l: 1101100
o: 1101111

That is not really correct. I've searched in the inet, for example, the binary of H

01001000 

There is one "0" missing. How can i fix this?

Murat Karagöz
  • 26,294
  • 9
  • 70
  • 97
  • Leading zero's are not significant. (Unless you are 007) 01 = 1 = 000000000000000000000001 right :) – online Thomas Jul 31 '17 at 09:04
  • You code is correct. You can drop a leading zero from binary, just as you can drop the leading zeros from the decimal number 00999 (999). – Michael Jul 31 '17 at 09:09

3 Answers3

1

Instead of binary = Integer.toBinaryString(character);

use the following expression:

binary = String.format("%8s", Integer.toBinaryString(character)).replace(' ', '0');
eparvan
  • 1,226
  • 10
  • 23
0

The missing zeroes are leading zeroes. A binary representation is numerical in essence, which means leading zeroes have no meaning. Therefore they are left out; the number 01001000 equals the number 1001000.

If you do want to print these leading zeroes, I suggest you solve this by using a string formatting function, that prints an exact number of digits regardless of the number's length.

Lee White
  • 3,367
  • 7
  • 30
  • 53
0

Another option would be using the DecimalFormat:

  private static String format( Integer value, String format )
  {
    DecimalFormat myFormatter = new DecimalFormat( format );
    return myFormatter.format( value );
  }

With this you can just reach in the Number and the format in which it should be displayed. Like this:

System.out.println(format( 1001000 , "00000000" ));

With 0 as format you say the number 0 has to displayed or the number that is reached in. Meaning if your format is longer than the actual number the leading places will be displayed as 0. If you read the JavaDoc you will find the section

Special Pattern Characters

There you can see an overview of the options for your format and what they will do.

EDIT:

As Olivier pointed out in the comments it is needed to convert it again. When you convert your Character with Integer.toBinaryString(character); you can just cast the String back to Long with Long.valueOf() and then reach it into to function or just refactor the method to:

  private static String format( String value, String format )
  {
    DecimalFormat myFormatter = new DecimalFormat( format );
    return myFormatter.format( Long.valueOf(value) );
  }
Nico
  • 1,508
  • 1
  • 17
  • 36
  • DecimalFormat has no support for binary. How do you do this whole stuff, then? – Olivier Grégoire Jul 31 '17 at 09:41
  • @OlivierGrégoire the answer got updated. It's pretty simple as he just needs to convert the binary representation from String to Integer again. – Nico Jul 31 '17 at 09:49
  • With the update, if I pass `"1010101010101010"`, it will fail. The OP passes `char`. A char can have up to 16 binary digits in their string representation. Your solution really limits it to 9. I would strongly recommend to amend the answer to make it usable with any char/integer. And in this case, `DecimalFormat` is of no use. The OP says he expects 8 digits, but with ints, it can go up to 32. This answer also doesn't explain why 8 digits might be too low. – Olivier Grégoire Jul 31 '17 at 09:56
  • It is of use. You can change it just a littlebit and it will work just fine. How about you [TIO](http://ideone.com/ENXw3e) . Remember to adjust the `format` String in order for DecimalFormat to work correctly. If your value is 12 elements long the format string has to be adjusted or the formatter will cut the value, which results in information loss. – Nico Jul 31 '17 at 10:03