std ::字符串到字节[]
std::string to BYTE[]
我的目标是获得:
BYTE Data1[] = {0x6b,0x65,0x79};
BYTE Data2[] = {0x6D,0x65,0x73,0x73,0x61,0x67,0x65};
,但我的起点是:
std::string msg = "message";
std::string key = "key";
我无法从std::string
到BYTE[]
。
我尝试了以下内容:
std::vector<BYTE> msgbytebuffer(msg.begin(), msg.end());
BYTE* Data1 = &msgbytebuffer[0];
这不会导致编译或运行时间错误。但是,最终结果(我将其馈送到Winapi函数-Crypto API)与我使用实际字节数组时的最终结果不一样({0x6D,0x65,0x73,0x73,0x61,0x67,0x65}
)。
您可以使用string::c_str()
函数,该功能将指针返回到C样式字符串,该函数可以传递给Winapi函数,例如:
foo(string.c_str());
它实际上要做的是它将指针返回到包含null终止字符序列的数组。
我想字节[]实际上是一个char数组。您可以通过:
将std ::字符串分配给字符阵列std::string str = "hello";
BYTE byte[6]; // null terminated string;
strcpy(byte, str.c_str()); // copy from str to byte[]
如果要在末端复制不带0的str,请改用strncpy
:
BYTE byte[5];
strncpy(byte, str.c_str(), str.length());
似乎我正在等待一个终止的c弦。您可以通过使用:
来实现这一目标msg.c_str();
或使用您的BYTE
类型,类似的东西:
std::vector<BYTE> msgbytebuffer(msg.length() + 1, 0);
std::copy(msg.begin(), msg.end(), msgbytebuffer.begin());
相关文章:
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 使用std::mt19937从字符串中返回一个随机单词
- 我可以重新分配/覆盖std::字符串吗
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 如何将这个std::字符串转换为std::基本字符串
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- std::字符串与 PCWSTR 的对话
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- 将C++ std::string 转换为 UTF-16-LE 编码的字符串
- 使用 std::string_view 的子字符串控制台输出
- std::regex:匹配由数字和空格组成的字符串,并提取数字.如何?
- 重载 std::字符串运算符+ 用于打印枚举名称
- 从std::字符串创建OssBitString
- C++ TCP 套接字 | 无法将"std::__cxx11::字符串"转换为"const
- C 中的特殊字符的子字符串(STD :: substr)
- 是否建议将字符串std::移动到将被覆盖的容器中
- 显式指定要写入的字符串 std::ostringstream
- 如何将uint16_t转换为宽字符串(std::wstring)
- 读取和Huffman压缩4字节二进制字符串STD C++Linux环境