Rule: The charcode of the Half-width space is 32, the Full-width space is 12288. The corresponding relationship between the other Half-width characters (33–126) and the whole angle (65281–65374) is: The difference is 65248.
Find a good law, the code is good to write:
var hash = {32:u3000};
Half-width full-angle
function Sbc2dbc (str) {
var ret = [], i = 0, len = str.length, code, CHR;
for (; I && Code < 127) {
CHR = Hash[code] = String.fromCharCode (code + 65248);
}
Ret[i] = CHR? Chr:str.charAt (i);
}
return Ret.join ();
}
Similarly:
var hash = {12288: };
Full-width turning half-width
function DBC2SBC (str) {
var ret = [], i = 0, len = str.length, code, CHR;
for (; I 65280 && Code < 65375) {
CHR = Hash[code] = String.fromCharCode (code-65248);
}
Ret[i] = CHR? Chr:str.charAt (i);
}
return Ret.join ();
}
The code above will also convert the symbol in the middle of 33-126. Most of the time, this is not what we need (like converting @ to @). The following code is less intrusive:
var hash = {};
Half angle turn whole angle. Convert only [0-9a-za-z]
function Sbc2dbc_w (str) {
var ret = [], i = 0, len = str.length, code, CHR;
for (; i < Len; ++i) {
code = str.charcodeat (i);
CHR = Hash[code];
if (!CHR &&
($ < code && Code < The
< code && Code < The
< code &A mp;& Code < 123)) {
CHR = Hash[code] = String.fromCharCode (code + 65248);
Ret[i] = CHR? Chr:str.charAt (i);
}
return Ret.join ();
}