Java: full-width characters to half-width characters

Source: Internet
Author: User

Java: full-width characters to half-width characters

Analysis:

Original string: 1234 abcabc
Fullwidth numeric fullwidth English halfwidth English

Converted string: 1234 abcabc

View rules by printing character encoding
1: B [1] =-1 B [2] =-1 B [3] = 17
2: B [1] =-1 B [2] =-1 B [3] = 18
3: B [1] =-1 B [2] =-1 B [3] = 19
4: B [1] =-1 B [2] =-1 B [3] = 20
A: B [1] =-1 B [2] =-1 B [3] = 65
B: B [1] =-1 B [2] =-1 B [3] = 66
C: B [1] =-1 B [2] =-1 B [3] = 67
A: B [1] =-1 B [2] = 0 B [3] = 97
B: B [1] =-1 B [2] = 0 B [3] = 98
C: B [1] =-1 B [2] = 0 B [3] = 99

The rule is as follows:

1. The third byte of the fullwidth character is-1.

2. the byte value difference between the full-width characters and the half-width characters is 32.

3. The third byte of the halfwidth character is 0.

Conversion Method: after determining that the third byte is-1, increase the fourth byte to 32, and then set the third byte to 0.

/* Fullwidth conversion */
Public String tohalf (string userinput ){
String oldstr = userinput;
String newstr = "";
String tmpstr = "";
Byte [] strbyte = NULL;
For (INT I = 0; I <oldstr. Length (); I ++ ){
Try {
Tmpstr = oldstr. substring (I, I + 1 );
Strbyte = tmpstr. getbytes ("Unicode ");
} Catch (Java. Io. unsupportedencodingexception e ){
E. printstacktrace ();
}
If (strbyte [2] =-1 ){
Strbyte [3] = (byte) (strbyte [3] + 32 );
Strbyte [2] = 0;
Try {
Newstr = newstr + new string (strbyte, "Unicode ");
} Catch (Java. Io. unsupportedencodingexception e ){
E. printstacktrace ();
}
} Else {
Newstr = newstr + tmpstr;
}
}
Return newstr;
}

The code is from the network, but the error is corrected.

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.