Javascript full-width conversion

Source: Internet
Author: User

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

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.