First, then talk nonsense^_^
The code copy is as follows:
/**
* Turn full-width characters
*/
function toDBC(str){
var result = "";
var len = str.length;
for(var i=0;i<len;i++)
{
var cCode = str.charCodeAt(i);
//The difference between the full and half-angle (except space): 65248 (decimal)
cCode = (cCode>=0x0021 && cCode<=0x007E)?(cCode + 65248): cCode;
//Processing 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);
//The difference between the full and half-angle (except space): 65248 (decimal)
cCode = (cCode>=0xFF01 && cCode<=0xFF5E)?(cCode - 65248): cCode;
//Processing 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 that the ASCII character range with full-width and half-width characters is: 0x20~0x7E.
for example:
The difference between half-width and full-width symbols
#0x00230xFF030xFEE0
?0x003F0xFF1F0xFEE0
Space 0x00200x030000x2FE0
Except spaces, in other characters, the whole and half-width are different: 0xFFE0
Therefore, in the character conversion of full-width and half-width, special processing of spaces is required.
For example:
Full-width = Half-width + 0xFEE0
Half-width = Full-width - 0xFFE0