SoFunction
Updated on 2025-04-10

JS full-width and half-width conversion example (share)

Recently, I am doing a form verification for the page of a PC website, and I need to convert the full-width input into a half-width symbol. I haven't learned about these coding knowledge before, so I still have to Google and check the information, so I'll briefly summarize it.

What is full-width, half-width

Traditionally, in computer systems used in English or Latin alphabet languages, each letter or symbol is stored in a byte space (one byte consists of 8 bits, totaling 256 encoding spaces); while in Chinese, Japanese and Korean characters, since the number of them exceeds 256, they are often used to store a character. In an environment where monospace fonts (such as DOS, partial text editors, etc.), Chinese, Japanese and Korean characters occupy twice the display width of Western characters at this time. Therefore, Chinese, Japanese, Korean and other characters are called full-width characters. In comparison, Latin letters or numbers are called half-width characters. Sometimes in order to make the font look neat, English letters, numbers and other symbols have also changed from taking up only one word space to displaying two words and using two bytes to storage format. (Wikipedia)

Transformation principle

Full-width space unicode encoded is 12288, half-width space is 32

The unicode encoding correspondence between half-width (33-126) and full-width (65281-65374) of other characters is: uniform difference of 65248

Turn half an angle in full

function ToCDB(str) {
    var tmp = "";
    for (var i = 0; i < ; i++) {
      if ((i) > 65248 && (i) < 65375) {
        tmp += ((i) - 65248);
      }
      else {
        tmp += ((i));
      }
    }
    return tmp
  }

Turn half a corner

function ToDBC(txtstring) {
    var tmp = "";
    for (var i = 0; i < ; i++) {
      if ((i) == 32) {
        tmp = tmp + (12288);
      }
      if ((i) < 127) {
        tmp = tmp + ((i) + 65248);
      }
    }
    return tmp;
  }

The above example of JS full-width and half-width transformation (sharing) is all the content I share with you. I hope you can give you a reference and I hope you can support me more.