I have encountered a broken string from another software source and I am attempting to repair its encoding using JavaScript, but I seem to be missing a crucial step.
Here is an example of the broken string: Détecté à lors ô ù
The desired output should be: Détecté à lors ôùi
Unfortunately, I am unaware of the encoding that was used to send me the string.
My plan involves leveraging the TextDecoder API to convert the string to bytes and then reencode it in either UTF-8 or UTF-16.
Below is the code snippet I utilized to identify the charset in use:
const str = 'Détecté à lors ôùi';
const str2 = 'Détecté à lors ô ù';
const charsets = [
'utf-8',
"ibm866",
"iso-8859-2",
// Add all other charsets here
];
// Rest of the code
(The code can be tested here: https://jsfiddle.net/tashebwj/)
The output generated by the code is as follows:
// Output results go here
Why is this method not functioning as intended? Are there any alternative approaches to fixing the string using this method or a different one?