在 C++ 中定义 UTF-16BE 字符串
我需要定义如下所示的 unicode 字符串:
const char SOME_STRING[] = { 0, 5, 0, 'M', 0, 'y', 0, 'S', 0, 't', 0, 'r' };
这是 UTF-16BE 字符串,前面带有包含长度的大端短字节,它在 java 中使用,这就是我需要它的用途。有没有比单独输入每个字符更好/更干净的方法来声明它?
I need to define unicode string that would look like so:
const char SOME_STRING[] = { 0, 5, 0, 'M', 0, 'y', 0, 'S', 0, 't', 0, 'r' };
This is UTF-16BE string prepended with big endian short containing length, it's used in java and that's what I need it for. Is there better/cleaner way to declare it than typing every character separately?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
wchar_t
代替,按需转换为字节,例如:You could use
wchar_t
instead, converting to bytes on demand, for example:C 和 C++ 没有定义它们所运行的平台的字节序性质如何工作。因此,语言本身无法将 16 位值序列声明为“大端”。
您所要做的就是获取平台本机字节序中的字符串。如果平台的字节序不是大字节序,则进行字节交换。您可以将字节交换版本存储在
std::vector
或类似文件中。C and C++ do not define how the endian nature of the platform they are running on works. Therefore, there is no way within the language themselves to declare a sequence of 16-bit values to be "big endian".
What you have to do is get the string in the platform native endian. Then do byte swapping if the endian-ness of the platform is not big endian. You can store the byte-swapped version in a
std::vector
or some such.