从 utf8 到 latin1
from utf8 to latin1
我有一个像这样的 utf-8 字符串:
=C3=A0=C3=A8=C3=AC=C3=B2=C3=B9
我需要将字符串解码为 latin1。预期结果是:
àèìòù
这是我尝试过但没有成功的方法:
utf8.decode(stringData.runes.toList())
我解决了。
感谢@lenz 评论和找到的 js 函数 here 我用这个函数解决了:
String decodeQuotedPrintable(String input) {
return input
// https://tools.ietf.org/html/rfc2045#section-6.7, rule 3:
// “Therefore, when decoding a `Quoted-Printable` body, any trailing white
// space on a line must be deleted, as it will necessarily have been added
// by intermediate transport agents.”
.replaceAll(RegExp(r'[\t\x20]$', multiLine: true), '')
// Remove hard line breaks preceded by `=`. Proper `Quoted-Printable`-
// encoded data only contains CRLF line endings, but for compatibility
// reasons we support separate CR and LF too.
.replaceAll(RegExp(r'=(?:\r\n?|\n|$)', multiLine: true), '')
// Decode escape sequences of the form `=XX` where `XX` is any
// combination of two hexidecimal digits. For optimal compatibility,
// lowercase hexadecimal digits are supported as well. See
// https://tools.ietf.org/html/rfc2045#section-6.7, note 1.
.replaceAllMapped(RegExp(r'=([a-fA-F\d]{2})'), (match) {
var codePoint = int.parse(match[1], radix: 16);
return String.fromCharCode(codePoint);
});
}
并使用此代码:
var output = decodeQuotedPrintable(input);
output = utf8.decode(latin1.encode(input));
我有一个像这样的 utf-8 字符串:
=C3=A0=C3=A8=C3=AC=C3=B2=C3=B9
我需要将字符串解码为 latin1。预期结果是:
àèìòù
这是我尝试过但没有成功的方法:
utf8.decode(stringData.runes.toList())
我解决了。
感谢@lenz 评论和找到的 js 函数 here 我用这个函数解决了:
String decodeQuotedPrintable(String input) {
return input
// https://tools.ietf.org/html/rfc2045#section-6.7, rule 3:
// “Therefore, when decoding a `Quoted-Printable` body, any trailing white
// space on a line must be deleted, as it will necessarily have been added
// by intermediate transport agents.”
.replaceAll(RegExp(r'[\t\x20]$', multiLine: true), '')
// Remove hard line breaks preceded by `=`. Proper `Quoted-Printable`-
// encoded data only contains CRLF line endings, but for compatibility
// reasons we support separate CR and LF too.
.replaceAll(RegExp(r'=(?:\r\n?|\n|$)', multiLine: true), '')
// Decode escape sequences of the form `=XX` where `XX` is any
// combination of two hexidecimal digits. For optimal compatibility,
// lowercase hexadecimal digits are supported as well. See
// https://tools.ietf.org/html/rfc2045#section-6.7, note 1.
.replaceAllMapped(RegExp(r'=([a-fA-F\d]{2})'), (match) {
var codePoint = int.parse(match[1], radix: 16);
return String.fromCharCode(codePoint);
});
}
并使用此代码:
var output = decodeQuotedPrintable(input);
output = utf8.decode(latin1.encode(input));