JavaScript realizes the conversion of Full-width and half-width characters _javascript skills

Source: Internet
Author: User

Let's go to the code first, talk nonsense ^_^

Copy Code code as follows:

/**
* Turn full angle character
*/
function Todbc (str) {
var result = "";
var len = str.length;
for (Var i=0;i<len;i++)
{
var ccode = str.charcodeat (i);
Difference between Full-width and half-width (extra space): 65248 (decimal)
Ccode = (ccode>=0x0021 && ccode<=0x007e)? (Ccode + 65248): Ccode;
Working with spaces
Ccode = (ccode==0x0020)? 0x03000:ccode;
result = String.fromCharCode (Ccode);
}
return result;
}
/**
* Turn Half-width characters
*/
function TOSBC (str) {
var result = "";
var len = str.length;
for (Var i=0;i<len;i++)
{
var ccode = str.charcodeat (i);
Difference between Full-width and half-width (extra space): 65248 (decimal)
Ccode = (ccode>=0xff01 && ccode<=0xff5e)? (cCode-65248): Ccode;
Working with spaces
Ccode = (ccode==0x03000)? 0x0020:ccode;
result = String.fromCharCode (Ccode);
}
return result;
}

Knowledge points

By comparing Half-width characters with full-width characters (ASCII characters), we can find the range of ASCII characters with Full-width and Half-width points: 0x20~0x7e.

Like what:

Total angle difference of symbol half angle
# 0x0023 0xff03 0xfee0
? 0x003f 0xff1f 0xfee0
Space 0x0020 0x03000 0X2FE0

In addition to space, the other characters, the full and half of the difference between the angle: 0xffe0

Therefore, in the Full-width and half-width character conversion, special handling of space is needed.

For example:

Full angle = half angle + 0xfee0

Half angle = Full angle-0xffe0

Related Article

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.