JS for full-width and half-angle verification, mutual transformation and introduction

Source: Internet
Author: User

1. What is full-width and half-width?

Full angle : is a computer character that refers to the position of a full-width character occupying two standard characters (or two half-width characters). The full angle occupies two bytes.

Chinese characters and the full-width of the English characters and the national standard gb2312-80 in the graphic symbols and special characters are full-width characters. In the full-width, letters and numbers, like Chinese characters, occupy the same wide position.

Half-width : Refers to a character occupying a standard character position. Half-width occupies one byte.

The half-width is the ASCII character, and the letters, numbers, and characters are half-angled when no Chinese input method is in effect.

Each half-width character occupies only one byte of space (one byte has 8 bits, a total of 256 encoded spaces). Chinese, Japanese, and Korean font size of ideographic language is much larger than 256 encoding space, so instead of two bytes to store. At the same time, because of the writing habits of Chinese, Japanese and Korean hieroglyphics, if the uniform use of full-width characters, the arrangement also appears neat.

In order to arrange neatly, English and other Latin characters and punctuation are also available in full-width format.

2. the difference between full angle and half angle

Full-width and half-width are mainly for punctuation, full-width punctuation is two bytes, and half-width is one byte. Both the half-width and the full-width of the Chinese characters account for two bytes.

3.js determines whether the input text is full-width or half-width?
Str= "Chinese;; a"     alert (Str.match (/[\u0000-\u00ff]/g))     / Half-width    alert (Str.match (/ [\u4e00-\u9fa5]/g)     // Chinese    alert (Str.match (/[\uff00-\uffff]/g))     // Full width   
4.js Mutual transformation of full-angle and half-angle

First, the following information should be clarified:

A. Full-width space is 12288, half-width space is 32

B. Other character half-width (33-126) and full-width (65281-65374) correspondence is: the difference between 65248

Half angle converted to full angle
function Todbc (txtstring) {     var tmp = "";       for (var i=0;i<txtstring.length;i++{         if(Txtstring.charcodeat (i) ==32) {             tmp= tmp+ string.fromcharcode (12288);         }          if (Txtstring.charcodeat (i) <127) {             tmp=tmp+string.fromcharcode (txtstring.charcodeat (i) +65248 );         }     }      return tmp;}

The above uses the JS charCodeAt () method and the fromCharCode () method.

The charCodeAt () method returns the Unicode encoding of the character at the specified position. This return value is an integer between 0-65535.

fromCharCode () can accept a specified Unicode value and then return a string.

To learn more about the charCodeAt () method and the fromCharCode () method, you can click the JavaScript charcodeat () method and the JavaScript fromCharCode () method.

Full-width conversion to half-width
functiontocdb (str) {varTMP = "";  for(vari=0;i<str.length;i++){         if(Str.charcodeat (i) = = 12288) {tmp+ = String.fromCharCode (Str.charcodeat (i)-12256); Continue; }        if(Str.charcodeat (i) > 65280 && str.charcodeat (i) < 65375) {tmp+ = String.fromCharCode (Str.charcodeat (i)-65248); }         Else{tmp+=String.fromCharCode (Str.charcodeat (i)); }     }     returntmp}

JS for full-width and half-angle verification, mutual transformation and introduction

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.