win32 字节数组到列表框中
win32 byte array into LISTBOX
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::ostringstream
或std::wostringstream
,并std::basic_string<TCHAR>
替换为std::string
或std::wstring
。
相关文章:
- C++如何生成std::数组列表
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- C++未知长度字符串的数组,其行为类似于 Python 字符串列表
- GTEST : 如何将数组列表作为参数传递到 ValuesIn of INSTANTIATE_TEST_CASE_P?
- 基于数组的列表 - 动态数组创建时出错
- 如何使用列表/数组在C++中实现 Python "... not in ..."?
- 从非托管 C# DLL 返回列表/数组
- C++对象列表数组
- 如何在C++中创建配对列表数组
- 链接列表数组
- 获取列表数组中的所有列表项
- 如何创建和扫描列表/数组..在嵌入式剪辑中
- 列表数组<string>,插入字符串行为奇怪
- 对 STL 列表数组使用迭代器的语法
- 在类|C++中创建分数列表数组
- C++是否有一些类似C#类型的东西来存储列表/数组中的类类型
- 列表数组-构造函数和main()中的地址不匹配
- c++中的列表数组.指针;/
- STL 列表数组无法访问列表函数
- 用动态内存管理创建类列表数组