Rule: The charcode of the halfwidth space is 32, and the fullwidth space is 12288. The correspondence between other halfwidth characters (33-126) and the fullwidth (65281-65374) is as follows: the differences are 65248.
Find the rule,CodeIt's easy to write:
VaR hash = {'32': '\ u3000'}; // function sbc2dbc (STR) {var ret = [], I = 0, Len = Str. length, code, CHR; For (; I <Len; ++ I) {code = Str. charcodeat (I); CHR = hash [Code]; If (! CHR & code> 31 & Code <127) {CHR = hash [Code] = string. fromcharcode (code + 65248);} RET [I] = CHR? CHR: Str. charat (I);} return ret. Join ('');}
Likewise:
VaR hash = {'000000': ''}; // function dbc2sbc (STR) {var ret = [], I = 0, Len = Str. length, code, CHR; For (; I <Len; ++ I) {code = Str. charcodeat (I); CHR = hash [Code]; If (! CHR & code> 65280 & Code <65375) {CHR = hash [Code] = string. fromcharcode (Code-65248);} RET [I] = CHR? CHR: Str. charat (I);} return ret. Join ('');}
The above Code also converts the symbols between 33-126. In many cases, this is not what we need (for example, converting @ @). The following code is more invasive:
VaR hash ={}; // returns the halfwidth to the fullwidth. Only Convert [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 & (47 <code & Code <58 | 64 <code & Code <91 | 96 <code & Code <123 )) {CHR = hash [Code] = string. fromcharcode (code + 65248);} RET [I] = CHR? CHR: Str. charat (I);} return ret. Join ('');}
Test page: sbc2dbc-test.html