如何解决c ++十六进制字符串集到字节数组不返回向量
所以基本上,我想创建一个函数
采用此十六进制模式:
"03 C6 8F E2 18 CA 8C E2 94 FD BC E5 03 C6 8F E2"
并返回以下字节数组:
BYTE pattern[] = { 0x03,0xC6,0x8F,0xE2,0x18,0xCA,0x8C,0x94,0xFD,0xBC,0xE5,0x03,0xE2 };
我的主要问题是我需要像我描述的那样,像输出数组的一个字节单元中的每个0x03一样
如果我使用这个
#include <windows.h>
std::vector<BYTE> strPatternToByte(const char* pattern,std::vector<BYTE> bytes)
{
std::stringstream converter;
std::istringstream ss( pattern );
std::string word;
while( ss >> word )
{
BYTE temp;
converter << std::hex << "0x" + word;
converter >> temp;
bytes.push_back( temp );
}
return bytes;
}
int main()
{
const char* pattern = "03 C6 8F E2 18 CA 8C E2 D4 FD BC E5 03 C6 8F E2";
std::vector<BYTE> bytes;
bytes = strPatternToByte(pattern,bytes);
BYTE somePtr[16];
for ( int i=0 ; i < 16 ; i++)
{
somePtr[i] = bytes[i];
}
for(unsigned char i : somePtr)
{
std::cout << i << std::endl;
}
/*
* output should be:
0x03
0xC6
0x8F
* etc
* .
* .
*/
return 0;
}
它实际上并没有执行我需要的操作,因为当我调试它时,我看着字节向量,我看到它将0放入一个单元格中,x放入一个单元格中,0放入单元格中,3放入单元格中,这是不是我想要的,反正有解决这种问题的方法吗? 输出不是应该的,我在代码中添加了输出,如下所示:
/*
* output should be:
0x03
0xC6
0x8F
* etc
* .
* .
*/
somePtr数组是我的最后一个输出,正如我所描述的那样。
谢谢。
解决方法
template <typename T>
std::vector<uint8_t> bytesFromHex(std::basic_istream<T>& stream,size_t reserve = 0x100)
{
std::vector<uint8_t> result;
result.reserve(reserve);
auto flags = stream.flags();
stream.setf(std::ios_base::hex,std::ios_base::basefield);
std::copy(std::istream_iterator<unsigned>{stream},{},std::back_inserter(result));
stream.flags(flags);
return result;
}
template <typename T>
std::vector<uint8_t> bytesFromHex(std::basic_string_view<T> s,size_t reserve = 0x100)
{
std::basic_istringstream<T> stream{std::basic_string<T>{s}};
return bytesFromHex(stream,reserve);
}
,
您有一些小错误。
通过重用converter
对象,它在第一次转换后将不再起作用,因为它进入了错误状态。尝试使用此版本,该版本每次在循环中都会重新创建stringstream
(您也可以在循环结束时调用converter.clear();
。
while (ss >> word)
{
int temp;
std::stringstream converter;
converter << std::hex << "0x" + word;
converter >> temp;
bytes.push_back(temp);
}
另外temp
应该是int
(毕竟您正在读取整数)。
输出循环错误,请尝试
for (int i : somePtr)
{
std::cout << "0x" << std::hex << std::setfill('0') << std::setw(2) << i << std::endl;
}
再次注意,i
是int
,并且我添加了格式以获得所需的效果。您需要#include <iomanip>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
typedef unsigned char BYTE; // Remove this line if you've included windows.h
std::vector<BYTE> strPatternToByte(std::string hex_chars) {
std::istringstream hex_chars_stream(hex_chars);
std::vector<BYTE> bytes;
unsigned c;
hex_chars_stream >> std::hex;
while (hex_chars_stream >> c) bytes.push_back(c);
return bytes;
}
int main() {
std::string hex_chars = "03 C6 8F E2 18 CA 8C E2 D4 FD BC E5 03 C6 8F E2";
auto bytes = strPatternToByte(hex_chars);
std::cout << std::setfill('0') << std::hex << std::uppercase;
for (unsigned i : bytes) std::cout << "0x" << std::setw(2) << i << '\n';
}
0x03
0xC6
0x8F
0xE2
0x18
0xCA
0x8C
0xE2
0xD4
0xFD
0xBC
0xE5
0x03
0xC6
0x8F
0xE2
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。