[英]How to convert from character positions to byte postions in UTF-8 file?
I have UTF-8 encoded text file. 我有UTF-8编码的文本文件。 I can read it by chars. 我可以用字符读它。 Each char can be either one byte or multibyte. 每个字符可以是一个字节或多字节。 How can I know where one byte was readen and whet it was readen more than one byte? 我怎么知道在哪里读取了一个字节而又在哪里读取了一个以上的字节?
Count the bytes while reading the char
s. 读取char
计算字节数。
For each char c
: 对于每个char c
:
if(c<128)
bytesCount++;
else if (c<2048)
bytesCount+=2;
else
bytesCount+=3;
See also encodeing definition wikipedia URF8 另请参见编码定义Wikipedia URF8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.