JavaScript function to convert UTF8 string between fullwidth and halfwidth forms
Apperently, you want to convert halfwidth and fullwidth form characters to their equivalent basic latin forms. If this is correct, you can do a replacement using a regular expression. Something like this should work:
var x = "!abc ABC!";
var y = x.replace(
/[\uff01-\uff5e]/g,
function(ch) { return String.fromCharCode(ch.charCodeAt(0) - 0xfee0); }
);
Where x is your input string and y is the output.
Year 2018 answer
Many years later – and it’s still impossible to find on the Internet a function that does this. So I wrote mine. (Nearly learned Japanese and Korean to get to this point.)
Simple version
Latin range only.
var shiftCharCode = Δ => c => String.fromCharCode(c.charCodeAt(0) + Δ);
var toFullWidth = str => str.replace(/[!-~]/g, shiftCharCode(0xFEE0));
var toHalfWidth = str => str.replace(/[!-~]/g, shiftCharCode(-0xFEE0));
Complete version
Let me know if I missed any character.
(function () {
let charsets = {
latin: {halfRE: /[!-~]/g, fullRE: /[!-~]/g, delta: 0xFEE0},
hangul1: {halfRE: /[ᄀ-ᄒ]/g, fullRE: /[ᆨ-ᇂ]/g, delta: -0xEDF9},
hangul2: {halfRE: /[ᅡ-ᅵ]/g, fullRE: /[ᅡ-ᅵ]/g, delta: -0xEE61},
kana: {delta: 0,
half: "。「」、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゙゚",
full: "。「」、・ヲァィゥェォャュョッーアイウエオカキクケコサシ" +
"スセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゛゜"},
extras: {delta: 0,
half: "¢£¬¯¦¥₩\u0020|←↑→↓■°",
full: "¢£¬ ̄¦¥₩\u3000│←↑→↓■○"}
};
let toFull = set => c => set.delta ?
String.fromCharCode(c.charCodeAt(0) + set.delta) :
[...set.full][[...set.half].indexOf(c)];
let toHalf = set => c => set.delta ?
String.fromCharCode(c.charCodeAt(0) - set.delta) :
[...set.half][[...set.full].indexOf(c)];
let re = (set, way) => set[way + "RE"] || new RegExp("[" + set[way] + "]", "g");
let sets = Object.keys(charsets).map(i => charsets[i]);
window.toFullWidth = str0 =>
sets.reduce((str,set) => str.replace(re(set, "half"), toFull(set)), str0);
window.toHalfWidth = str0 =>
sets.reduce((str,set) => str.replace(re(set, "full"), toHalf(set)), str0);
})();
/* Example starts here: */
var set = prompt("Enter a couple of comma-separated strings (half or full-width):",
["aouäöü123", "'\"?:", "¢£¥₩↑→", "コンニチハ", "ᄀ까ᅢ"].join()).split(",");
var steps = [set, set.map(toFullWidth), set.map(toFullWidth).map(toHalfWidth)];
var tdHTML = str => `<td>${str}</td>`;
var stepsHTML = steps.map(step => step.map(tdHTML).join(""));
var rows = document.getElementsByTagName("tr");
[...rows].forEach((row,i) => row.insertAdjacentHTML("beforeEnd", stepsHTML[i]));
th, td {border: 1px solid lightgrey; padding: 0.2em;}
th {text-align: left;}
table {border-collapse: collapse;}
<table>
<tr><th scope="row">Input:</th></tr>
<tr><th scope="row">Full-width:</th></tr>
<tr><th scope="row">Half-width:</th></tr>
</table>
Try this
function toASCII(chars) {
var ascii = '';
for(var i=0, l=chars.length; i<l; i++) {
var c = chars[i].charCodeAt(0);
// make sure we only convert half-full width char
if (c >= 0xFF00 && c <= 0xFFEF) {
c = 0xFF & (c + 0x20);
}
ascii += String.fromCharCode(c);
}
return ascii;
}
// example
toASCII("ABC"); // returns 'ABC' 0x41