开发者

How to calculate length of an UCS-2 string and its size in C++?

开发者 https://www.devze.com 2022-12-21 00:21 出处:网络
I have a string in UCS-2 encoding.开发者_Go百科 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.

I have a string in UCS-2 encoding.开发者_Go百科 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?


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/.


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".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号