如何在 C++ 中计算 UCS-2 字符串的长度及其大小?
我有一个 UCS-2 编码的字符串。我需要将此字符串复制到另一个 UCS-2 字符串。在复制之前,我需要计算用于内存分配的 UCS-2 字符串的长度。
如何计算 UCS-2 字符串的长度?
I have a string in UCS-2 encoding. I need to copy this string to another UCS-2 string. Before copying I need to calculate the length of a UCS-2 string for memeory allocation.
How to calculate length of an UCS-2 string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UCS2 不携带有关字符串长度本身的信息。您的原始字符串表示形式要么是以 0 结尾,在这种情况下您只需检查 0 代码单元(即 16 位 0 值),要么它不是以 0 结尾,在这种情况下您需要一些 out-of-带有关字符串长度的信息(例如单独存储的缓冲区大小)。
一般来说,UCS 2 是 UTF-16 的子集 (http://www.unicode .org/faq/basic_q.html#14)。因此,您应该可以使用 UTF-16 工具,例如 ICU 库 http://site.icu-项目.org/。
UCS2 does not carry information about string length itself. Your original string representation either is 0-terminated, in which case you can just check for a 0 code unit (i.e. a 16-bit 0 value), or it is not 0-terminated, in which case you need some out-of-band information about the length of the string (such as a separately stored buffer size).
In general, UCS 2 is a subset of UTF-16 (http://www.unicode.org/faq/basic_q.html#14). Hence, you should be fine with UTF-16 tools, such as the ICU library http://site.icu-project.org/.
这取决于您使用的字符串类型。如果没有类型(只是已知包含 UCS-2 编码字符串的内存缓冲区),则必须知道它是如何表示的。它可以有一个前置长度计数,或者像普通的旧
char *
“C strings”一样以 0 结尾。That depends on the string type you're using. If there is no type (just a memory buffer known to contain a string in UCS-2 encoding), you have to know how it was represented. It could have a prepended length count, or be 0-terminated just like plain old
char *
"C strings".