0

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)

  • 1
    You can never get 3992613786 as an "int result" in java because that number is simply out of range of the data type int. (Hence why when you convert your Long to int you get a wrong result). Do you really need an `int` as a result? Otherwise just use `long` – OH GOD SPIDERS Mar 08 '22 at 15:54
  • Actually I do not know. I am not supposed to change the java code, only write it in python, but since there is no comment in the original java code, I am not sure what the calcul is about. I edited my message and added the following line in the java code : I get that it is about reversing bytes, but I have no idea how to do this in pyhton. I can't even figure out how to get the "int" result in python, so... – theonewhodidnotknow Mar 09 '22 at 08:32

3 Answers3

1

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.

Lunatic
  • 1,519
  • 8
  • 24
  • Read more about casting and primitives, non-primitives in here https://www.javatpoint.com/primitive-vs-non-primitive-data-structure – Lunatic Mar 08 '22 at 15:55
0
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

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
0

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