Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

method String.getBytes() is big endian or litter endian?

I need to send String to client socket, for right sequence, the endian is important but I not saw endian information in source code.Does it needn't care about or just I skipped those code?

like image 781
LoranceChen Avatar asked Feb 29 '16 16:02

LoranceChen


People also ask

What is getBytes () in Java?

getBytes() This function takes no arguments and used the default charset to encode the string into bytes. getbytes() function in java is used to convert a string into a sequence of bytes and returns an array of bytes. Syntax: public byte[] getBytes()

Which of the following are correct variants of getBytes () method?

There are three variants of getBytes() method. The signature or syntax of string getBytes() method is given below: public byte[] getBytes() public byte[] getBytes(Charset charset)

How do you know if you are big or little endian?

Specifically, little-endian is when the least significant bytes are stored before the more significant bytes, and big-endian is when the most significant bytes are stored before the less significant bytes. When we write a number (in hex), i.e. 0x12345678 , we write it with the most significant byte first (the 12 part).

How are strings stored in big-endian?

In the Big-Endian storage order, the "big", most significant, byte 0x44 is stored "first", at the lowest memory location 101. The 0x44 displays to the left of the other bytes, which follow in ascending left-to-right memory address order: 102, 103, 104.


1 Answers

getBytes() uses the system's default charset, which means basically all bets are off. It could be big-endian UTF-16, little-endian UTF-16, UTF-8, ISO-8859-1... basically anything.

If you need to specify endianness, or anything about the charset for that matter, you should use getBytes(Charset) or getBytes(String). There are a few standard charsets that all JREs support — including UTF_16BE (big endian) and UTF_16LE (little endian).

like image 171
yshavit Avatar answered Sep 22 '22 13:09

yshavit