最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to convert half byte character to full byte character japanese - Stack Overflow

matteradmin5PV0评论

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

ウマンクナイ

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

ウマンクナイ
Share Improve this question edited May 24, 2024 at 9:44 Penny Liu 17.6k5 gold badges86 silver badges108 bronze badges asked Mar 6, 2020 at 5:13 umang naikumang naik 1392 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

To convert half-width katakana to full-width katakana, you can simply use the string method normalize with parameter 'NFKC' (Normalization Form KC):

let str = 'アシタハイイテンキカナ、ブーヴー';
console.log(str.normalize ('NFKC')); // -> 'アシタハイイテンキカナ、ブーヴー'

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アイウエオガギグゲゴ'));

Post a comment

comment list (0)

  1. No comments so far