c ++十六进制字符串集到字节数组而不是return向量

c++ hex set of strings to array of bytes not return vector

基本上,我想创建一个函数

采用此十六进制模式:

"03 C6 8F E2 18 CA 8C E2 94 FD BC E5 03 C6 8F E2"

和returns这个字节数组:

BYTE pattern[] = { 0x03, 0xC6, 0x8F, 0xE2, 0x18, 0xCA, 0x8C, 0xE2, 0x94, 0xFD, 0xBC, 0xE5, 0x03, 0xC6, 0x8F, 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);
}

https://godbolt.org/z/aW915b

#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';
}

Output :

0x03
0xC6
0x8F
0xE2
0x18
0xCA
0x8C
0xE2
0xD4
0xFD
0xBC
0xE5
0x03
0xC6
0x8F
0xE2

您有一些小错误。

通过重复使用您的 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>