1

having this issue with a decryption program I am writing in Java. Here is the code in question

public static int int_to_int(int input)
{
    int[] value_array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
            21, 22, 23, 24, 25, 26};
    int[]bin_array= {00000, 00001, 00010, 00011,
            00100, 00101, 00110, 00111,
            01000, 01001, 01010, 01011, 01100,
            01101, 01110,  01111, 10000, 10001,
            10010,  10011, 10100, 10101, 10110,
            10111, 11000,11001, 11010, 11011};
    for(int i=0; i <27; i++)
    {
        System.out.println("hello");
        if(input==value_array[i])
        {
            System.out.println("returning: " + bin_array[i] + "at: " + i);
            return bin_array[i];
        }

    }
    return -1;
}

And here is the issue highlighted in a line

double temp = 00010;
    System.out.println("returning: " + temp);

This will output

returning: 8

but I want to see

returning: 00010

thoughts?

  • 4
    wilx has explained why you're getting 8 - but what numeric value would you expect to produce "00010"? Do you understand that a number is just a number, and doesn't remember a particular textual representation? If you have `int x = 0x10;` that's *exactly* the same value as `int x = 16;`. – Jon Skeet May 21 '16 at 06:23
  • You are also misrepresenting the facts. `double temp = 00010` will not output as `8`, but as `8.0`. – Andreas May 21 '16 at 06:30
  • @Andreas lol, sorry. was troubleshooting on my own and didn't double check – Alex McAfee May 21 '16 at 06:34
  • I would recommend to add a prefix `0b` to numbers to indicate them as Binary... Leading 0s will make it Octal – Swanand May 21 '16 at 07:11

3 Answers3

5

The 00010 is octal number, i.e., 8. Remove all the leading zeroes.

wilx
  • 17,697
  • 6
  • 59
  • 114
  • Cool, thanks, I can't remove all leading zeroes as they need to be XOR'd later on but some other answers on here will give me the options I need. Thank you for clarifying this. – Alex McAfee May 21 '16 at 06:34
  • 1
    That'd make it a decimal 10, but from the context I assumed that OP means to use binary, as indicated by the name bin_array and the sequence of numbers in that array. – Arjan May 21 '16 at 06:34
  • Leading zeroes will always be zeroes. – wilx May 21 '16 at 06:34
5

Integers prefixed with 0 are treated as octal, not binary. Prefix with 0b or 0B to indicate binary, like 0B00010. To print as binary, use

System.out.println("returning: " + Integer.toBinaryString(temp));

or,

System.out.println("returning: " + Integer.toString(temp, 2));

That is, assuming temp is an integer, like in your bin_array.

Arjan
  • 823
  • 1
  • 7
  • 18
0
 public static int int_to_int(int input)
    {
        int[] value_array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
                21, 22, 23, 24, 25, 26};
        int[]bin_array= {00000, 00001, 00010, 00011,
                00100, 00101, 00110, 00111,
                01000, 01001, 01010, 01011, 01100,
                01101, 01110,  01111, 10000, 10001,
                10010,  10011, 10100, 10101, 10110,
                10111, 11000,11001, 11010, 11011};
        for(int i=0; i <27; i++)
        {
            System.out.println("hello");
            if(input==value_array[i])
            {
                System.out.println("returning: " + Integer.toOctalString(bin_array[i]) + "at: " + i);
                return bin_array[i];

            }
        }
        return -1;
    }

I use Integer.toOctalString

Moolerian
  • 534
  • 6
  • 18