我有一個uint8_t數組中的USB字符串描述符。例如:如何在cout/cerr上打印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'。 – PaulMcKenzie
你知道'uint16_t'字節是什麼嗎?例如UTF-16? – Mine
我不知道......這是我編寫的USB代碼,但描述符被定義爲彙編語言'.string16「abcd」'。 hexdump正是我在內存緩衝區中所擁有的。 我試過std :: wcout(per @PaulMcKenzie),但我得到了一堆?分數。 –