The characters in the Java program, whether Chinese or English, all occupy two bytes. We are all familiar with this conclusion, but why?
It is actually because of the char type.
It's like double byte.
Unsigned integer
.
UTF-16 Coding
It is the same as Unicode encoding, except that the UTF-16 will add "Fe FF" two bytes before the code stream during encoding, indicating that the characters are stored in UTF-16 format, at the time of reading, the program will know that it is a UTF-16-encoded character.
Chinese character to UTF-16
:
Byte [] encodearr = string. valueof (chinesechar). getbytes ("UTF-16 ");
Digress:
When writing a file to a UTF-16, the file encoding mark Fe FF is also
Write the file at the beginning, but cannot write a code to write a file at the time of writing, because every time in the UTF-16 encoding, will generate the encoding mark, and write the file also written, this requires only the first time to write a file to the need of the flag is stored for multiple times, so when the UTF-16 to store characters, it is best to encoding once completed before writing, or when there are too many characters, we can use outputstreamwriter for packaging to easily write data multiple times instead of writing the file encoding mark multiple times. Or we can use Word throttling to store data and encode the data, however, from the second encoding to writing a file, it is also possible to manually remove the file encoding mark and then store it again, but it is not as convenient as writing it multiple times in a batch stream.
There is no write problem with the MP3 tag we are studying. However, if you want to read the tag information correctly, please take a look!
// Read the encoded UTF-8 file in byte streams
Fileinputstream Fi = new fileinputstream ("...");
Byte [] B = new byte [1024];
Int readcount = Fi. Read (B );
// Manually discard the content of the first three bytes
System. Out. println (new string (B, 3, readcount-3, "UTF-8 "));
Character UTF-8 characters are compatible with Unicode encoding. Chinese characters are encoded in UTF-8 and take up to three bytes.
The English UTF-8 encoding rule template is 0 xxxxxxx
Chinese template
Yes: 1110 XXXX 10 xxxxxx 10 xxxxxx
For example, the Unicode encoding of "medium" is 4e2d. 4e2d is between 0800-ffff, so we need to use the above three-byte rule template to write 4e2d as binary: 0100 1110 0010, divide the bitstream into 0100 111000 101101 based on the three-byte template, and replace X in the template in sequence. The result is as follows: 1110-0100 10-111000 10-101101, that is, E4 B8 ad, this is the UTF-8 encoding of the "medium" character.
I want to emphasize that:
When you use a Java program to import characters into a file in the form of UTF-8 encoding, it will not add the encoding mark in the file header. However, if
Using a variety of tools to use quiet listen, convert MP3 files for UTF-8 storage, will write the file encoding mark ef bb bf in the file opening Header
Three bytes length encoding mark, if the use of byte stream to read the UTF-8 file with the encoding mark, there will be a problem, that is why we manually converted to UTF-8, but reading Korean and Japanese... the cause of the garbled error still occurs. In this case, only the first three bytes of content can be read from the byte stream.