How do you convert a string to a 32-bit big-endian number in Java?
-
25-09-2019 - |
Question
I'm trying to get some websockets working and I need to convert a string (or a long) to a 32-bit big-endian number.
How do you do this in Java?
Solution
Java writes out values in big-endian/network order so you're in luck in that regard.
Java int values are always signed 32-bit value too.
Regarding sending a long in 32-bits, be sure the value fits in 32 bits or you're going to lose data. You're just not able to send 5 gallons in a 2.5 gallon bucket.
You can use the Integer.parseInt(String) method to get an int value:
int x = Integer.parseInt("12345");
or Integer.valueOf(String) if you want an Integer.
Integer x = Integer.valueOf("12345");
OTHER TIPS
Integer.valueOf(str);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow