Question

I'm trying to make a UUencode algorithm and part of my code contains this:

for(int x = 0; x < my_chars.length; x++)
{
   if((x+1) % 3 == 0)
   {
   char first = my_chars[x-2];
   char second = my_chars[x-1];
   char third = my_chars[x];
   int first_binary = Integer.parseInt(Integer.toBinaryString(first));
   int second_binary = Integer.parseInt(Integer.toBinaryString(second));
   int third_binary = Integer.parseInt(Integer.toBinaryString(third));
   int n = (((first << 8) | second) << 8) | third;
   System.out.print(my_chars[x-2] + "" + my_chars[x-1] + my_chars[x] + Integer.toBinaryString(n));

   }
}

System.out.println();
System.out.println(Integer.toBinaryString('s'));

What I'm trying to achieve is to combine those 8 bits from the chars that I get into a big 24 bits int. The problem I'm facing is that the result is a 23 bit int. Say my first 3 chars were:

  • 'T' with a binary representation of 01010100
  • 'u' with a binary representation of 01110101
  • 'r' with a binary representation of 01110010

The result that I get from my program is a int formed from these bits: 10101000111010101110010 Which is missing the 0 at the beginning from the representation of 'T'.

Also I have included the last 2 lines of code because the binary string that I get from 's' is: 1110011 which is missing the 0 at the beginning.

I have checked if I scrolled by mistake to the right but it does not seem that I have done so.

Was it helpful?

Solution

The method Integer.toBinaryString() does not zero-pad the results on the left; you'll have to zero-pad it yourself.

This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top