简体   繁体   English

如何在 javascript 中将 ASCII 字符串转换为 UTF-8

[英]How to convert ASCII string to UTF-8 in javascript

I need to convert an ASCII string to UTF-8 with javascript.我需要使用 javascript 将 ASCII 字符串转换为 UTF-8。

So the following raw ASCII strings:所以下面的原始 ASCII 字符串:

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.寻找一种使用 javascript 函数的方法。

You can actually use this utf-8 library for that.您实际上可以为此使用此utf-8库。 If you are using plain javaScript you need to save the utf-8.js file and use it in <script src=""> tag.如果您使用的是普通的javaScript ,则需要保存utf-8.js文件并在<script src="">标记中使用它。 Then you can use utf8.decode() function.然后你可以使用utf8.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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM