首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Javascript:如何从utf-8到iso-8859-1的编码和解码

Javascript:如何从utf-8到iso-8859-1的编码和解码
EN

Stack Overflow用户
提问于 2020-04-24 08:48:14
回答 1查看 711关注 1票数 0

我在UTF8中有一个文本,我想将其转换为iso-8859-1。另外,我有一个iso-8859-1格式的文本,我想编码成UTF8。

我认为使用本机函数是不可能的,那么有没有好的库可以做到这一点呢?我在浏览器上用的是纯javascript,不是nodejs,也不是用的是webpack等等。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-04-24 09:14:17

除了escape之外,还需要使用decodeURIComponent才能在UTF8/ ISO-8859-1之间来回切换

有关它的更多信息,请参阅this article

解码UTF-:fixedstring = decodeURIComponent(escape(utfstring));

解码ISO-8859-1:utfstring = unescape(encodeURIComponent(originalstring));

要查看字符串是否为UTF-8,请执行以下操作:

代码语言:javascript
运行
AI代码解释
复制
var fixedstring;

try{
    // If the string is UTF-8, this will work and not throw an error.
    fixedstring=decodeURIComponent(escape(badstring));
}catch(e){
    // If it isn't, an error will be thrown, and we can assume that we have an ISO string.
    fixedstring=badstring;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61404481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档