JS Tutorial: JavaScript full half angle conversion

Source: Internet
Author: User

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 ();
}


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.