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.