How to calculate length of an UCS-2 string and its size in C++?
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".
精彩评论