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...

5
  • java ... i am sorry i forgot to mention that . Commented Dec 5, 2012 at 14:46
  • Casting long to int in Java should not produce negative numbers. How do you do the cast? Commented Dec 5, 2012 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 Commented Dec 5, 2012 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 ... Commented Dec 5, 2012 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. Commented Dec 6, 2012 at 7:26

1 Answer 1

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

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

8 Comments

it is not integer ... in my file they are characters in binary form ... parseLong returns a positive number because it does exactly this ...
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.
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 ...
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 "&#171;i&#212;&#171;" in binary : 10101011011010011101010010101011 this binary is this integer : 2875839659 ... that is what i want to do with bitwise and masks ...
the objective is to convert this binary to integer ... with the opposite way that this method which i wrote in my question does ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.