Weird mysql latin1 Encoding

Source: Internet
Author: User

Mysql latin1 is not equal to the standard latin1 (iso-8859-1) and cp1252, 0x80-0x9f characters more than the iso-8859-1, than cp1252 0x81, 0x8d, 0x8f, 0x90, 0x9d has a total of five characters.

Http://dev.mysql.com/doc/refman/5.0/en/charset-we-sets.html

Latin1 is the default character set. mySQL's latin1 is the same as the Windows cp1252 character set. this means it is the same as the official ISO 8859-1 or IANA (Internet Assigned Numbers Authority) latin1, could t that IANA latin1 treats the code points between 0x80 and 0x9f as "undefined," whereas cp1252, and therefore MySQL's latin1, assign characters for those positions. for example, 0x80 is the Euro sign. for the "undefined" entries in cp1252, MySQL translates 0x81 to Unicode 0x0081, 0x8d to 0x008d, 0x8f to 0x008f, 0x90 to 0x0090, and 0x9d to 0x009d.

 

In Java, garbled characters may occur if standard iso-8859-1 or cp1252 decoding is used.
S. getBytes ("iso-8859-1") or s. getBytes ("cp1252 ");

I wrote a piece of code to solve this problem.
Private String convertCharset (String s ){
If (s! = Null ){
Try {
Int length = s. length ();
Byte [] buffer = new byte [length];
// 0x81 to Unicode 0x0081, 0x8d to 0x008d, 0x8f to 0x008f, 0x90 to 0x0090, and 0x9d to 0x009d.
For (int I = 0; I <length; ++ I ){
Char c = s. charAt (I );
If (c = 0 x0 0081 ){
Buffer [I] = (byte) 0x81;
}
Else if (c = 0x008d ){
Buffer [I] = (byte) 0x8d;
}
Else if (c = 0x008f ){
Buffer [I] = (byte) 0x8f;
}
Else if (c = 0x0090 ){
Buffer [I] = (byte) 0x90;
}
Else if (c = 0x009d ){
Buffer [I] = (byte) 0x9d;
}
Else {
Buffer [I] = Character. toString (c). getBytes ("cp1252") [0];
}
}
String result = new String (buffer, "UTF-8 ");
Return result;
} Catch (UnsupportedEncodingException e ){
Logger. error ("charset convert error", e );
}
}
Return null;
}

From James's Thoughts
 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.