Search code examples
javascripttypescriptunicodecjk

How to convert half byte character to full byte character japanese


I have to try to convert each input on text box that converts to a full byte character please find below my code...

 static convertToFullWidth(string: any) {
    if(string){
      var listOfCharacters = '';
      for (let index = 0; index < string.length; index++) {
        var selectedElement = string[index].charCodeAt(0);
        if (0x0020 < selectedElement && selectedElement < 0x007F) {
          selectedElement = 0xFF00 + (selectedElement - 0x0020);
        }
        if (0x0020 === selectedElement) {
          selectedElement = 0x3000;
        }
        listOfCharacters += String.fromCharCode(selectedElement);
      }
      return listOfCharacters;
    }
  }

Attempts:

Working input

ウマングナイ

Not Working

ウマンクナイ

Solution

  • I'm not sure if there's a mathematical pattern to the unicode codepoints that you could use here. It seems there isn't consistency between one and the other.

    This page provides a simple approach, which is just to have a mapping from half-width to full-width and use a regex replace on the matched characters:

    function hankana2Zenkana(str) {
        var kanaMap = {
            'ガ': 'ガ', 'ギ': 'ギ', 'グ': 'グ', 'ゲ': 'ゲ', 'ゴ': 'ゴ',
            'ザ': 'ザ', 'ジ': 'ジ', 'ズ': 'ズ', 'ゼ': 'ゼ', 'ゾ': 'ゾ',
            'ダ': 'ダ', 'ヂ': 'ヂ', 'ヅ': 'ヅ', 'デ': 'デ', 'ド': 'ド',
            'バ': 'バ', 'ビ': 'ビ', 'ブ': 'ブ', 'ベ': 'ベ', 'ボ': 'ボ',
            'パ': 'パ', 'ピ': 'ピ', 'プ': 'プ', 'ペ': 'ペ', 'ポ': 'ポ',
            'ヴ': 'ヴ', 'ヷ': 'ヷ', 'ヺ': 'ヺ',
            'ア': 'ア', 'イ': 'イ', 'ウ': 'ウ', 'エ': 'エ', 'オ': 'オ',
            'カ': 'カ', 'キ': 'キ', 'ク': 'ク', 'ケ': 'ケ', 'コ': 'コ',
            'サ': 'サ', 'シ': 'シ', 'ス': 'ス', 'セ': 'セ', 'ソ': 'ソ',
            'タ': 'タ', 'チ': 'チ', 'ツ': 'ツ', 'テ': 'テ', 'ト': 'ト',
            'ナ': 'ナ', 'ニ': 'ニ', 'ヌ': 'ヌ', 'ネ': 'ネ', 'ノ': 'ノ',
            'ハ': 'ハ', 'ヒ': 'ヒ', 'フ': 'フ', 'ヘ': 'ヘ', 'ホ': 'ホ',
            'マ': 'マ', 'ミ': 'ミ', 'ム': 'ム', 'メ': 'メ', 'モ': 'モ',
            'ヤ': 'ヤ', 'ユ': 'ユ', 'ヨ': 'ヨ',
            'ラ': 'ラ', 'リ': 'リ', 'ル': 'ル', 'レ': 'レ', 'ロ': 'ロ',
            'ワ': 'ワ', 'ヲ': 'ヲ', 'ン': 'ン',
            'ァ': 'ァ', 'ィ': 'ィ', 'ゥ': 'ゥ', 'ェ': 'ェ', 'ォ': 'ォ',
            'ッ': 'ッ', 'ャ': 'ャ', 'ュ': 'ュ', 'ョ': 'ョ',
            '。': '。', '、': '、', 'ー': 'ー', '「': '「', '」': '」', '・': '・'
        };
    
        var reg = new RegExp('(' + Object.keys(kanaMap).join('|') + ')', 'g');
        return str
                .replace(reg, function (match) {
                    return kanaMap[match];
                })
                .replace(/゙/g, '゛')
                .replace(/゚/g, '゜');
    };
    
    console.log(hankana2Zenkana('アシタハイイテンキカナ、ブーヴー'));

    It also provides a function for English letters and Hindu-Arabic numerals:

    function hankaku2Zenkaku(str) {
      return str.replace(/[A-Za-z0-9]/g, function(s) {
        return String.fromCharCode(s.charCodeAt(0) + 0xFEE0);
      });
    }
    
    console.log(hankaku2Zenkaku('123abC')); // '123abC'

    You can chain these together to get your desired result:

    function hankana2Zenkana(str) {
      var kanaMap = {
        'ガ': 'ガ',
        'ギ': 'ギ',
        'グ': 'グ',
        'ゲ': 'ゲ',
        'ゴ': 'ゴ',
        'ザ': 'ザ',
        'ジ': 'ジ',
        'ズ': 'ズ',
        'ゼ': 'ゼ',
        'ゾ': 'ゾ',
        'ダ': 'ダ',
        'ヂ': 'ヂ',
        'ヅ': 'ヅ',
        'デ': 'デ',
        'ド': 'ド',
        'バ': 'バ',
        'ビ': 'ビ',
        'ブ': 'ブ',
        'ベ': 'ベ',
        'ボ': 'ボ',
        'パ': 'パ',
        'ピ': 'ピ',
        'プ': 'プ',
        'ペ': 'ペ',
        'ポ': 'ポ',
        'ヴ': 'ヴ',
        'ヷ': 'ヷ',
        'ヺ': 'ヺ',
        'ア': 'ア',
        'イ': 'イ',
        'ウ': 'ウ',
        'エ': 'エ',
        'オ': 'オ',
        'カ': 'カ',
        'キ': 'キ',
        'ク': 'ク',
        'ケ': 'ケ',
        'コ': 'コ',
        'サ': 'サ',
        'シ': 'シ',
        'ス': 'ス',
        'セ': 'セ',
        'ソ': 'ソ',
        'タ': 'タ',
        'チ': 'チ',
        'ツ': 'ツ',
        'テ': 'テ',
        'ト': 'ト',
        'ナ': 'ナ',
        'ニ': 'ニ',
        'ヌ': 'ヌ',
        'ネ': 'ネ',
        'ノ': 'ノ',
        'ハ': 'ハ',
        'ヒ': 'ヒ',
        'フ': 'フ',
        'ヘ': 'ヘ',
        'ホ': 'ホ',
        'マ': 'マ',
        'ミ': 'ミ',
        'ム': 'ム',
        'メ': 'メ',
        'モ': 'モ',
        'ヤ': 'ヤ',
        'ユ': 'ユ',
        'ヨ': 'ヨ',
        'ラ': 'ラ',
        'リ': 'リ',
        'ル': 'ル',
        'レ': 'レ',
        'ロ': 'ロ',
        'ワ': 'ワ',
        'ヲ': 'ヲ',
        'ン': 'ン',
        'ァ': 'ァ',
        'ィ': 'ィ',
        'ゥ': 'ゥ',
        'ェ': 'ェ',
        'ォ': 'ォ',
        'ッ': 'ッ',
        'ャ': 'ャ',
        'ュ': 'ュ',
        'ョ': 'ョ',
        '。': '。',
        '、': '、',
        'ー': 'ー',
        '「': '「',
        '」': '」',
        '・': '・'
      };
    
      var reg = new RegExp('(' + Object.keys(kanaMap).join('|') + ')', 'g');
      return str
        .replace(reg, function(match) {
          return kanaMap[match];
        })
        .replace(/゙/g, '゛')
        .replace(/゚/g, '゜');
    };
    
    function hankaku2Zenkaku(str) {
      return str.replace(/[A-Za-z0-9]/g, function(s) {
        return String.fromCharCode(s.charCodeAt(0) + 0xFEE0);
      });
    }
    
    function allhankaku2Zenkaku(str) {
      return hankaku2Zenkaku(hankana2Zenkana(str));
    }
    
    console.log(allhankaku2Zenkaku('abcde01234アイウエオガギグゲゴ'));