Search code examples
javajvmendianness

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?


Solution

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