简体   繁体   中英

How do you convert a string to a 32-bit big-endian number in Java?

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?

Integer.valueOf(str);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM