我有一些文件,其中包含一堆不同种类的二进制数据,我正在写一个模块来处理这些文件。在
其中,它包含以下格式的UTF-8编码字符串:2字节big-endianstringLength(我用它来解析解包结构())然后是字符串。因为它是UTF-8,所以字符串的字节长度可能会大于stringLength,如果字符串包含多字节字符,那么read(stringLength)就会很短(更不用说搞乱文件中的所有其他数据)。在
如果知道UTF-8的多字节属性,如何从文件中读取nUTF-8字符(不同于n字节)?我在谷歌上搜索了半个小时,所有的结果要么不相关,要么是我无法做出的假设。在
给定一个文件对象和若干字符,可以使用:
测试结果:
^{pr2}$UTF-8中的一个字符可以是1字节、2字节、3字节3。在
如果必须逐字节读取文件,则必须遵循UTF-8编码规则。http://en.wikipedia.org/wiki/UTF-8
大多数时候,您只需将编码设置为utf-8,然后读取输入流。在
你不需要关心你读了多少字节。在
相关问题 更多 >
编程相关推荐