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