以字节为单位计算的UTF-8字符串大小?字符串、字节、大小、单位

2023-09-07 12:39:37 作者:◆◆芣②杺◆◆

我有一个字符串的 UTF8

我需要得到它的大小。 (字节)

I need to get its size. ( bytes)

总是做它 X2 ? //我的意思是乘以2 的

有没有.NET功能呢?

is there any .net function for this ?

P.S。

我要问这个问题监守我最新的question ...我需要(在mempry映射文件)calc下的串偏移(UTF8) - 从另一个进程。的

im asking this question becuase of my latest question ...I need ( in mempry mapped file) to calc the offset of a string ( utf8 ) - from another process..

推荐答案

没有,它并不总是根据实际内容X2为UTF-8和变化。为ASCII字符它是1字节,但也可以写入多个字节为大$ C $的c-点值。你想:

No, it is not always x2 for UTF-8 and changes based on the actual contents. For ASCII characters it is 1 byte, but can go into several bytes for large code-point values. You want:

string s = // your string
int len = Encoding.UTF8.GetByteCount(s);