[英]C++ cast byte array from stringified bytes
我有一個非常大的字符串,如下所示:
std :: string tmpString =“0xC7,0x04,0x33,0xC0,0x49,0x74,0x0A,...”
我想將每個字符串化的字節轉換回一個字節數組。 什么是這種情況的最佳解決方案?
我正在使用VC ++
嘗試這個:
std::string tmpString = "0xC7,0x04,0x33,0xC0,0x49,0x74,0x0A,...";
...
std::vector<BYTE> bytes;
bytes.reserve((tmpString.length() / 5) + 1);
std::istringstream iss(tmpString);
std::string s;
while (std::getline(iss, s, ',')) {
WORD num; // istreamstream does not have an '>>' operator for bytes
std::istringstream(s) >> std::hex >> num;
bytes.push_back(BYTE(num));
}
std::string str = ("0xC7,0x04,0x33,0xC0,0x49,0x74,0x0A");
char *p2str = str.c_str();
std::vector<BYTE> numVect;
char *p2c;
do
int num = strtol(p2str,&p2c,16);
p2str = p2c + 1; // skiping the ,
numVect.push_back(BYTE(num));
while(*p2c);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.