1

Here's is the situation ... i have a binary file which contains 32-bit binary strings of characters (e.g 1011011100100110101010101011010 ) and i want to convert this to integer ... I have already tried to do it with parse-Int but if the most significant value is 1, i get back a negative number and i do not want that ... Then i tried it with parse-Long and it was okay but after that when i get this integer i have to send it to another class which can receive only integers , as a result i do casting from long to int and i get back a negative integer again ... The only way to do that is with a piece of code that i found which does the opposite thing ( from int to string ) but i do not understand how to change-convert it. It is about masks which i do not know a lot of things.

Here is the code :

    private static String intToBitString(int n) {
         StringBuffer sb = new StringBuffer();
         for (int mask = 1 << 31; mask != 0; mask = mask >>> 1)
             sb.append((n & mask) == 0 ? "0" : "1");
         return sb.toString();
     }

Thank you in advance...

Ian Overton
  • 1,060
  • 7
  • 17
  • java ... i am sorry i forgot to mention that . – Marios Filipidis Dec 05 '12 at 14:46
  • Casting long to int in Java should not produce negative numbers. How do you do the cast? – Jakub Zaverka Dec 05 '12 at 15:12
  • i have the number converted by parseLong ... and i type : String a = x.next(); int base = 2; long integer = Long.parseLong(a,base); // converted and positive int Finalinteger = (int) integer; // negative – Marios Filipidis Dec 05 '12 at 15:42
  • because of the length of the integer it produces ... because actually this number is long ... as a result i cannot store a long number in a integer ... that is why casting does not work ... – Marios Filipidis Dec 05 '12 at 16:01
  • What are you trying to achieve is impossible in Java. In Java an int is a signed-integer, meaning 31-bits and a sign bit. The Integer.MAX_VALUE is 2^31-1. A bigger value can't fit in an integer. Perhaps the receiving class needs modification so that it accepts a long. Or using a signed integer is OK after all. – kgiannakakis Dec 06 '12 at 07:26

1 Answers1

2

An integer with the highest bit set to 1 is a negative integer, regardless of the number of bits. Just add the heading zero to the string or alternatively clear the highest bit with bitwise AND (x & 0x7FFFFFFF). You can only store a 31 bit positive integer in java int.

Even if you assign such value to long (long x = 0xFFFFFFFF, will be -1), the sign bit expands and now you have the negative long (you can write 0x0FFFFFFFFL however to assign the expected 00000000FFFFFFFF to long). You still need to clear the high bits if this is unwanted behavior.

    int a = 0x80000007; // High bit 1 - negative!
    long b = a; // Sign expands!
            // Clearing high bits (mind leading 0 and 
            // the long type suffix (L) in the hex constant:
    long c = b & 0x0FFFFFFFFL; 
    System.out.println(a + ":" + Long.toHexString(b) + ":"
            + Long.toHexString(c));

The output: -2147483641:ffffffff80000007:80000007

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • it is not integer ... in my file they are characters in binary form ... parseLong returns a positive number because it does exactly this ... – Marios Filipidis Dec 05 '12 at 14:50
  • If the highest bit of the positive 32 bit value is one, you cannot store it in java int. Unsigned int type would be required, but Java does not have such. Store in long then. While assigning from int to long, the sign bit will expand and the long will also be negative. Simply clear the highest bits of your long as previously explained. – Audrius Meškauskas Dec 05 '12 at 14:52
  • actually i do not know how i am going to clear the highest bit with bitwise ... i do not know a lot of things in java but i need this converter for another project that is why i am asking... i just have to convert a binary file to integers and then send them to another class via .setWord which can recieve only integers ... – Marios Filipidis Dec 05 '12 at 14:53
  • Thank you very much for the code but the problem is that i want the output to be a positive integer ... for example : this is "«iÔ«" in binary : 10101011011010011101010010101011 this binary is this integer : 2875839659 ... that is what i want to do with bitwise and masks ... – Marios Filipidis Dec 05 '12 at 15:27
  • the objective is to convert this binary to integer ... with the opposite way that this method which i wrote in my question does ... – Marios Filipidis Dec 05 '12 at 15:34
  • Your implementation is correct; if you take in 32 ones and zeroes, then any result that will be an `int` could be negative or nonnegative. That's what happens in a language with signed integers. It's your idea that these numbers should be nonnegative that is wrong. – Louis Wasserman Dec 05 '12 at 17:40
  • Why don't you want negative numbers? What are you using these numbers for? What does the receiving class do? – Diego Basch Dec 06 '12 at 08:37
  • the receiving class actually takes only int because is a memory class. I send this converted string with a method which is called set.Word and it takes only integers .. Actually i found a way that i can do that but still i have some problems ... Lets say that i have this 32 bits string : 10010101011010101001010000110110 ... if i find a way to somehow scan this string and find the positions that there is 1 instead of 0 then i can continue and finish this. – Marios Filipidis Dec 07 '12 at 13:06