How to convert ASCII string to UTF-8 in javascript

Viewed 57

I need to convert an ASCII string to UTF-8 with javascript.

So the following raw ASCII strings:

imagᅢᄄ-thrᅢ랡.png
imᅢᄂge-twᅢᄊ.png
ᅢᆲmᅢᄀgᅢᄅ-fᅢ배ᄏr.png

Needs to be converted to:

imäge-twö.png
imagè-thréê.png
ìmágé-fòûr.png

I can do it with this online utility (so it's possible!). Looking for a way to do it with a javascript function.

1 Answers

You can actually use this utf-8 library for that. If you are using plain javaScript you need to save the utf-8.js file and use it in <script src=""> tag. Then you can useutf8.decode() function. Something like:

<body>
    <script src="./utf-8.js"></script>
    <script>
        console.log(utf8.decode('imagᅢᄄ-thrᅢ랡.png'))
        console.log(utf8.decode('imᅢᄂge-twᅢᄊ.png'))
        console.log(utf8.decode('ᅢᆲmᅢᄀgᅢᄅ-fᅢ배ᄏr.png'))
    </script>
</body>
Related