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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.