'Parsing string long to int with java and python

I am trying to convert some java code to python. I have a problem with the following java lines :

int toto = (int)Long.parseLong("11101101111110100111001110011010",2);
String result = Integer.reverseBytes(toto);

In java, i get those results :

Long.parseLong("11101101111110100111001110011010",2) = 3992613786
(int)Long.parseLong("11101101111110100111001110011010",2) = -302353510
Integer.reverseBytes(toto) = -1703675155

I think I understand the first line, but why is there a "(int)" here ? What is it supposed to do ? (I'm a beginner in java and in byte management, and I couldn't find any documentation that I understand regarding that point).

In python, I managed to get the first result by converting to base 2 (I found this out totally by chance) :

int("11101101111110100111001110011010",2) = 3992613786

But how can I obtain the "int" result, and then the "reverse byte" result in python ?

(By the way, I use Python 3)



Solution 1:[1]

In the end, I used the answer from gengkev here.

By playing around, I found something similar to BreizhGatch's function, but that doesn't require a conditional statement. n & 0x80000000 extracts the 32-bit sign bit; then, the - keeps the same 32-bit representation but sign-extends it; finally, the extended sign bits are set on n.

def toSigned32(n):

n = n & 0xffffffff return n | (-(n & 0x80000000))

Solution 2:[2]

The parseLong() method of Java Long class is used to parse the CharSequence argument as a signed long with the specified radix , beginning at a specified beginIndex and extending to endIndex-1, with observing the emthod signuture you will find out the parseLong will return long and it need to be cast to integer if you need it as integer.

public static long parseLong(CharSequence s, int beginIndex, int endIndex, int radix)

(Integer), (int) or generally (T) is for casting to required class type.

Solution 3:[3]

Long.parseLong("11101101111110100111001110011010",2) = 3992613786

The previous line takes the binary in the form of string, and evaluates the binary into a 10base number which then is assigned into a long variable.

The following line

(int)Long.parseLong("11101101111110100111001110011010",2)

just casts the assigned value which exists inside the long variable into an int value.

Keep in mind though that this is not a safe operation, since the long variable consists of 8 bytes of information and is able to hold a number in the range of (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)

On the other side int variable consists of 4 bytes of information and is able to hold a number in the range of (-2,147,483,648 to 2,147,483,647)

You can use the following to understand why this is not safe

    long a = Long.MAX_VALUE;
    System.out.println(a); // Prints 9223372036854775807
    int b = (int) a;
    System.out.println(b);  // Prints -1

The biggest positive long value when casted to int variable, it becomes the negative -1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Lunatic
Solution 3