【发布时间】:2016-10-17 03:19:17
【问题描述】:
我在 uint8_t 数组中有一个 USB 字符串描述符。例如:
0000:12 03 34 00 45 00 36 00 31 00 42 00 43 00 30 00 ..4.E.6.1.B.C.0.
0010:30 00 0.
(前两个字节是长度和描述符类型;其余字节是 uint16_t 字符。)
我想尽可能少地在终端上打印它,最好不要乱搞所有其他打印(就像cout << "Hello, world" << endl;)
我特别想做:
cout << "Serial number is: " << some_cast_or_constructor( buf + 2, len - 2 ) << endl;
对于上面的字符串描述符,在终端上获取以下内容:
Serial number is: 4E61BC00
这可能吗,还是我必须深入研究 Unicode 奥秘?
[编辑添加:]
根据@PaulMcKenzie,我尝试了这个程序:
#include <iostream>
#include <fstream>
#include <exception>
#include <string>
#include <locale>
int
main( int argc, char **argv )
{
char buf[] = { 34, 00, 45, 00, 36, 00, 31, 00, 42, 00, 43, 00, 30, 00, 30, 00 };
std::wcout << "Hello" << std::wstring( (const wchar_t *)buf, sizeof(buf) ) << std::endl;
return 0;
}
输出:
user:/tmp$ g++ foo.cc
user:/tmp$ ./a.out
Hello??????????
user:/tmp$
【问题讨论】:
-
使用
std::wcout,而不是std::cout。 -
你知道
uint16_t字节是什么吗?例如 UTF-16? -
我不确定...这是我编写的 USB 代码,但描述符定义为汇编语言
.string16 "abcd"。 hexdump 正是我在内存缓冲区中的内容。我尝试了 std::wcout (根据@PaulMcKenzie),但我得到了一堆?标记。 -
在 Linux (Debian)、gcc-4.9.2 上运气不好。在 MacOSX 上,我得到
Hello[nothing]。我想是时候进行一些挖掘了。 (我猜这很可能是一个终端问题。)
标签: c++ unicode character-encoding stdio