win32 字节数组到列表框中

win32 byte array into LISTBOX

本文关键字:列表 数组 字节 字节数 win32      更新时间:2023-10-16

i have BYTE[6] = {54, 61, 6c, 6f}. 如何将此字节数组添加为LISTBOX项以显示54616c6f? LISTBOX 始终将此十六进制值转换为 ASCII 字符,也使用重新转换 Byte 数组。

将字节数组转换为十六进制字符串非常简单。您可以使用std::ostringstream,例如:

#include <sstream>
#include <iomanip>
std::basic_string<TCHAR> toHex(BYTE bytes[], int count)
{
std::basic_ostringstream<TCHAR> oss;
for (int i = 0; i < count; ++i){
oss << std::hex << std::noshowbase << (WORD)bytes[i];
}
return oss.str();
}

现场演示

如果 ListBox 控件具有LBS_HASSTRINGS样式,则可以使用LB_ADDSTRING消息向其添加字符串,例如:

BYTE bytes[6] = {0x54, 0x61, 0x6c, 0x6f};
std::basic_string<TCHAR> hexStr = toHex(bytes, 4);
SendMessage(hListBox, LB_ADDSTRING, 0, reinterpret_cast<LPARAM>(hexStr.c_str()));

如果您直接使用基于 Win32 ANSI/Unicode 的 API,而不是使用基于TCHAR的 API,则只需根据需要将std::basic_ostringstream<TCHAR>替换为std::ostringstreamstd::wostringstream,并std::basic_string<TCHAR>替换为std::stringstd::wstring