- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 UTF-16 字节数组 (&[u8]
),我想在 Rust 中将其解码并重新编码为 UTF-8。
在Python中我可以这样做:
array.decode('UTF-16', errors='ignore').encode('UTF-8')
如何在 Rust 中做到这一点?
最佳答案
这里的问题是 UTF-16 是为 16 位单元定义的,并且没有指定如何将两个 8 位单元(又名字节)转换为一个 16 位单元。
因此,我假设您使用的是网络字节序(即大字节序)。请注意,这可能不正确,因为 x86 处理器使用小端。
因此重要的第一步是转换 u8
进入u16
。在这种情况下,我将迭代它们,通过 u16:from_be_bytes()
转换它们。 ,然后将它们收集到向量中。
然后,我们可以使用 String::from_utf16()
或 String::from_utf16_lossy()
转换Vec<u16>
进入String
.
String
s 在 Rust 中内部表示为 UTF-8。所以我们可以通过 .as_bytes()
直接拉出UTF-8表示或 .into_bytes()
.
fn main() {
let utf16_bytes: &[u8] = &[
0x00, 0x48, 0x20, 0xAC, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x20, 0x00, 0x77, 0x00,
0x6f, 0x00, 0x72, 0x00, 0x6c, 0x00, 0x64, 0x00, 0x21,
];
let utf16_packets = utf16_bytes
.chunks(2)
.map(|e| u16::from_be_bytes(e.try_into().unwrap()))
.collect::<Vec<_>>();
let s = String::from_utf16_lossy(&utf16_packets);
println!("{:?}", s);
let utf8_bytes = s.as_bytes();
println!("{:?}", utf8_bytes);
}
"H€llo world!"
[72, 226, 130, 172, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
请注意,我们必须使用 .try_into().unwrap()
在我们的map()
功能。这是因为.chunks_exact()
不会让编译器知道我们迭代的 block 有多大。
一旦稳定下来,就会出现 array_chunks()
方法确实让编译器知道,并且会使代码更短、更快。遗憾的是,它仅在 nightly
中可用。现在。
#![feature(array_chunks)]
fn main() {
let utf16_bytes: &[u8] = &[
0x00, 0x48, 0x20, 0xAC, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x20, 0x00, 0x77, 0x00,
0x6f, 0x00, 0x72, 0x00, 0x6c, 0x00, 0x64, 0x00, 0x21,
];
let utf16_packets = utf16_bytes
.array_chunks()
.cloned()
.map(u16::from_be_bytes)
.collect::<Vec<_>>();
let s = String::from_utf16_lossy(&utf16_packets);
println!("{:?}", s);
let utf8_bytes = s.as_bytes();
println!("{:?}", utf8_bytes);
}
> cargo +nightly run
"H€llo world!"
[72, 226, 130, 172, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
这假设我们的输入完全可以转换为 u16
单位。在生产代码中,建议检查字节数是否奇数。
为了通过错误处理正确编写此内容,我会将其提取到方法中并传播错误:
use thiserror::Error;
#[derive(Error, Debug)]
enum ParseUTF16Error {
#[error("UTF-16 data needs to contain an even amount of bytes")]
UnevenByteCount,
#[error("The given data does not contain valid UTF16 data")]
InvalidContent,
}
fn parse_utf16(data: &[u8]) -> Result<String, ParseUTF16Error> {
let data16 = data
.chunks(2)
.map(|e| e.try_into().map(u16::from_be_bytes))
.collect::<Result<Vec<_>, _>>()
.map_err(|_| ParseUTF16Error::UnevenByteCount)?;
String::from_utf16(&data16).map_err(|_| ParseUTF16Error::InvalidContent)
}
fn main() {
let utf16_bytes: &[u8] = &[
0x00, 0x48, 0x20, 0xAC, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x20, 0x00, 0x77, 0x00,
0x6f, 0x00, 0x72, 0x00, 0x6c, 0x00, 0x64, 0x00, 0x21,
];
let s = parse_utf16(utf16_bytes).unwrap();
println!("{:?}", s);
let utf8_bytes = s.as_bytes();
println!("{:?}", utf8_bytes);
}
"H€llo world!"
[72, 226, 130, 172, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
关于arrays - 如何将 UTF-16 字节数组重新编码为 UTF-8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73176253/
UTF-8、UTF-16 和 UTF-32 之间有何区别? 据我所知,它们都将存储 Unicode,并且每个都使用不同数量的字节来表示字符。选择其中之一是否有优势? 最佳答案 当 ASCII 字符代表
好的。我知道这看起来像典型的“他为什么不直接用谷歌搜索或去 www.unicode.org 查一下?”问题,但对于这样一个简单的问题,在检查了两个来源后,我仍然无法回答。 我很确定这三种编码系统都支持
是否存在可以用 UTF-16 编码但不能用 UTF-8 编码的字符 最佳答案 没有。 UTF-* 是可以对全范围 Unicode 字符进行编码的编码。 编码之间的差异在于每个字符使用多少字节。 关于u
是否存在可以用 UTF-16 编码但不能用 UTF-8 编码的字符 最佳答案 没有。 UTF-* 是可以对全范围 Unicode 字符进行编码的编码。 编码之间的差异在于每个字符使用多少字节。 关于u
UTF-16 是一种双字节字符编码。交换两个字节的地址将产生 UTF-16BE 和 UTF-16LE。 但我发现在 Ubuntu gedit 文本编辑器中存在名称 UTF-16 编码,以及 UTF-1
我想将 UTF-16 字符串转换为 UTF-8。我通过 Unicode 发现了 ICU 库。我在转换时遇到问题,因为默认设置是 UTF-16。我试过使用转换器: UErrorCode myError
UTF-16 需要 2 个字节,UTF-8 需要 1 个字节。 而USB是面向8bit的,UTF-8更自然。 UTF-8 向后兼容 ASCII,而 UTF-16 则不然。 UTF-16 需要 2 个字
我对将 unicode 字符转换为十六进制值有点困惑。 我正在使用这个网站获取字符的十六进制值。 ( https://www.branah.com/unicode-converter ) 如果我输入“
我已经用UTF-8编码创建了一个文件,但是我不了解其在磁盘上占用的大小的规则。这是我的完整研究: 首先,我创建了一个带有印地语字母“'”的文件,Windows 7上的文件大小为 8个字节。 现在带有两
如何将WideString(或其他长字符串)转换为UTF-8中的字节数组? 最佳答案 这样的功能将满足您的需求: function UTF8Bytes(const s: UTF8String): TB
我有一个奇怪的验证程序,用于验证utf-8字符串是否是有效的主机名(PHP中的Zend Framework主机名valdiator)。它允许IDN(国际化域名)。它将比较每个子域与由其十六进制字节表示
在 utf16 和 utf32 中,一个字节的零是否意味着空?就像在 utf8 中一样,还是我们需要 2 个和 4 个字节的零来相应地在 utf16 和 utf32 中创建 null? 最佳答案 在
这是基于我的观察,对于 mysql,默认字符集 utf8 有点误导,它不支持完整的 Unicode,因为它无法存储四字节 UTF-8 编码的字符。它实际上是 utf8mb4 字符集,它是完整的 Uni
我只有处理 ASCII(单字节字符)的经验,并且阅读了很多关于人们如何以不同方式处理 Unicode 的帖子,这些帖子提出了他们自己的一系列问题。 此时我对 Unicode 的了解非常有限,我读到过U
我明白 std::codecvt在 C++11 中执行 UTF-16 和 UTF-8 之间的转换,并且 std::codecvt执行 UTF-32 和 UTF-8 之间的转换。是否可以在 UTF-8
我正在编写一个 HTTP 服务器并使用 trivial-utf-8:write-utf-8-bytes 来响应请求。我听说Babel就像trivial-utf-8但效率更高,所以我想试一试。搜索了一段
我正在设计一个新的 CMS,但想要设计它来满足我 future 的所有需求,比如多语言内容,所以我认为 Unicode (UTF-8) 是最好的解决方案 但是通过一些搜索我得到了这篇文章 http:/
例如,假设我在字符串中有以下 xml: 如果我尝试将其插入到带有 Xml 列的 SQL Server 2005 数据库表中,我将收到以下错误(我使用的是 EF 4.1,但我认为这无关紧要): XM
我正在使用 Python CSV 库读取两个 CSV 文件。 一种使用 UTF-8-BOM 编码,另一种使用 UTF-8 编码。在我的实践中,我发现使用“utf-8-sig”作为编码类型可以读取这两个
假设我的数据库设置如下以使用 utf-8(mysql 中的完整 4mb 版本) mysql_query("SET CHARACTER SET utf8mb4"); mysql_query("SET N
我是一名优秀的程序员,十分优秀!