Vala:从无法识别多字节字符的字节中读取 UTF-8 字符串
Vala: Reading UTF-8 string from bytes not recognizing multibyte characters
对于我目前正在处理的应用程序,我需要从二进制文件中读取 UTF-8 编码的字符串。这些字符串不是以 null 结尾的,而是以指定其长度的字节开头。
当我尝试读入这样的字符串时,所有多字节 UTF-8 字符都变成 ?
。在下面找到一个示例:
public void main(string[] args) {
File file = File.new_for_path("test.bin");
DataInputStream instream = new DataInputStream(file.read());
uint8[] chars = new uint8[instream.read_byte()];
instream.read(chars);
print(@"$((string) chars)\n");
}
这当然是剥离样本。有问题的实际二进制文件是加密的,这里没有反映出来。如果我将它与包含字节序列 09 52 C3 AD 61 73 74 72 61 64
或 Ríastrad
的示例文件 test.bin 一起使用,并以 UTF-8 字节长度开头。因此,预期输出为 Ríastrad
,但实际输出为 R?astrad
.
是否有人能够阐明这个问题,也许还有解决方案?
您需要将 Intl.setlocale ();
添加到您的代码中:
public void main(string[] args) {
Intl.setlocale ();
File file = File.new_for_path("test.bin");
DataInputStream instream = new DataInputStream(file.read());
uint8[] chars = new uint8[instream.read_byte()];
instream.read(chars);
print(@"$((string) chars)\n");
}
print ()
的默认语言环境是 C 语言环境,即 US ASCII。美国 ASCII 字符范围之外的任何字符都显示为 ?
。使用 Intl.setlocale ();
将语言环境设置为与机器 运行 程序相同。
对于我目前正在处理的应用程序,我需要从二进制文件中读取 UTF-8 编码的字符串。这些字符串不是以 null 结尾的,而是以指定其长度的字节开头。
当我尝试读入这样的字符串时,所有多字节 UTF-8 字符都变成 ?
。在下面找到一个示例:
public void main(string[] args) {
File file = File.new_for_path("test.bin");
DataInputStream instream = new DataInputStream(file.read());
uint8[] chars = new uint8[instream.read_byte()];
instream.read(chars);
print(@"$((string) chars)\n");
}
这当然是剥离样本。有问题的实际二进制文件是加密的,这里没有反映出来。如果我将它与包含字节序列 09 52 C3 AD 61 73 74 72 61 64
或 Ríastrad
的示例文件 test.bin 一起使用,并以 UTF-8 字节长度开头。因此,预期输出为 Ríastrad
,但实际输出为 R?astrad
.
是否有人能够阐明这个问题,也许还有解决方案?
您需要将 Intl.setlocale ();
添加到您的代码中:
public void main(string[] args) {
Intl.setlocale ();
File file = File.new_for_path("test.bin");
DataInputStream instream = new DataInputStream(file.read());
uint8[] chars = new uint8[instream.read_byte()];
instream.read(chars);
print(@"$((string) chars)\n");
}
print ()
的默认语言环境是 C 语言环境,即 US ASCII。美国 ASCII 字符范围之外的任何字符都显示为 ?
。使用 Intl.setlocale ();
将语言环境设置为与机器 运行 程序相同。