C++ 如何将字符串附加到 BYTE 数组
C++ How to append a string to BYTE array?
我在C++中有以下代码(用Visual Studio 2010编写(。
void TEST(BYTE data[], int size)
{
wstring aData = L"Here is my string";
//something code to append aData string to data array
WinHttpClient client(url);
// Send HTTP post request.
client.SendHttpRequest(L"POST");
}
如何将数据字符串附加到数据字节数组。
简短的回答:你不能。
所以你得到了 BYTE[] 和大小,但你不知道数据是在堆栈上还是堆上,所以你不能只用realloc
或其他东西来让它变大。
一种方法是确保数组大于所需大小,并传递当前和最大大小。
另一种方法是更改 API 以允许您使用 realloc
或类似来调整数组大小。
我能想到的唯一不更改 API 的方法是在 data
中使用某种标记来分隔已用和未使用的空间(不适用于二进制数据(。 例如,0 表示未使用,因此您可以查找第一个0
并从那里开始追加。
编辑也许我误读了意图。如果您不想进行永久更改,则可以获取本地副本,附加到该副本(并确保将其清理干净(。我以为你想要一个永久的"就地"附加。
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <boost/locale/encoding_utf.hpp>
std::wstring utf8_to_wstring(const std::string& str)
{
return utf_to_utf<wchar_t>(str.c_str(), str.c_str() + str.size());
}
int main()
{
unsigned char test[10];
std::string tesStr((char*)test);
wstring temp = utf8_to_wstring(tesStr);
}
相关文章:
- C++如何将字符数组转换为字节向量(向量<byte>)
- C++ 无法将 std::vector<BYTE> 转换为字符串或字符数组
- 三维数组中的C/C++DWORD到BYTE和BYTE到DWORD的转换
- 如何将字符串加载到BYTE*数组(C++)的元素中
- 将 BYTE 数组转换为无符号长整型
- 如何在Windows上将BYTE数组映射为FILE *
- 将 BSTR 转换为 BYTE 数组C++
- Visual C++ 2010 - 转换 10 GB BYTE 数组的最快方法?
- 如何将 BYTE 数组中的两个字节存储为 int(或类似的东西)
- 将opengl纹理保存到Byte数组中
- 将 byte[] 数组作为方法的多个参数传递
- Android & JNI 如何将 Byte[] 数据数组传递给 JNI 并取回 Byte []
- 从 BYTE 数组读取 32 位整数.VC++
- C++ 如何将字符串附加到 BYTE 数组
- 如何将 BYTE 数组转换为字符数组以使用套接字 C++ 发送
- 正在将托管Byte数组转换为非托管字符串
- 函数返回BYTE数组
- 将unsigned char(BYTE)数组转换为const t_wchar* (LPCWSTR)
- 如何将HBITMAP转换为JNI的BYTE数组
- C++ 如何从文件创建 byte[] 数组(我不是说逐字节读取文件)?