Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function toWindows1252(string, replacement)
{
var ret = new Array(string.length);
var i, ch;
replacement = typeof replacement === "string" && replacement.length > 0 ? replacement.charCodeAt(0) : 0;
for (i = 0; i < string.length; i++)
{
ch = string.charCodeAt(i);
if (ch <= 0x7F || (ch >= 0xA0 && ch <= 0xFF))
{
ret[i] = ch;
}
else
{
ret[i] = toWindows1252.table[string[i]];
if (typeof ret[i] === "undefined")
{
ret[i] = replacement;
}
}
}
return ret;
}
toWindows1252.table = {
'\x81': 129, '\x8d': 141, '\x8f': 143, '\x90': 144,
'\x9d': 157, '\u0152': 140, '\u0153': 156, '\u0160': 138,
'\u0161': 154, '\u0178': 159, '\u017d': 142, '\u017e': 158,
'\u0192': 131, '\u02c6': 136, '\u02dc': 152, '\u2013': 150,
'\u2014': 151, '\u2018': 145, '\u2019': 146, '\u201a': 130,
'\u201c': 147, '\u201d': 148, '\u201e': 132, '\u2020': 134,
'\u2021': 135, '\u2022': 149, '\u2026': 133, '\u2030': 137,
'\u2039': 139, '\u203a': 155, '\u20ac': 128, '\u2122': 153
};
document.write(toWindows1252('Hello World àèéìòù'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment