Javascript full-width Problem

Source: Internet
Author: User
Today, coding found that the system needed to automatically turn the full angle to the half angle, and found the following from the Internet.
Check full-width half-width
VaR S = "babd1232133 ";
VaR strcode;
For (VAR I = 0; I <S. length; I ++)
{
Strcode = S. charcodeat (I );
If (strcode> 65248) | (strcode = 12288) // 12288 is a space
{
Alert ("full! ");
Break;
}
}
The halfwidth character is saved by one byte, And the fullwidth character is saved by two bytes. Therefore, if the absolute value of the code value is smaller than 256, It is the halfwidth character. Otherwise, it is the fullwidth character.
Functions of full-width to half-width conversion
Function dbc2sbc (STR)
{

VaR result = '';
For (VAR I = 0; I <Str. length; I ++ ){
Code = Str. charcodeat (I); // obtain the Unicode code of the Current Character
If (code> = 65281 & Code <= 65373) // In this unicode encoding range, all English letters and characters are included.
{
Result + = string. fromcharcode (Str. charcodeat (I)-65248 );
// Convert the Unicode code of the full-width character to the Unicode code of the corresponding half-width character
} Else if (code = 12288) // Space
{
Result + = string. fromcharcode (Str. charcodeat (I)-12288 + 32 );
} Else
{
Result + = Str. charat (I );
}
}
Return result;
}
Javascript is a 16-bit code.
From 65281 to 65373, only the data here is the full angle. There must be a range limit.
This is the case with 65281 algorithms.
For example, the full-width character
Encoded as ff21
Convert to hexadecimal Yes
1111 1111 0010 0001
Take the decimal value for the two bytes.
The first byte is
65280
The second byte is
33
So a's 65313

Subtract this encoding
Half-width

65248 smaller than 65280 32
Therefore, if the ASC code is smaller than 32, it cannot be converted.
Because the first byte converted to character encoding cannot be 1111 1111 or ff.
It will be smaller than this, so it should not be full-width.
That is to say, the first byte cannot satisfy all 1

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.