簡體   English   中英

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");
}

我如何將aData字符串附加到數據BYTE數組。

簡短的回答:不能。

因此,您可以傳遞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);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM