mardi 22 septembre 2020

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

So basically , I want to create a function that

takes this hex pattern:

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

and returns this array of bytes:

BYTE pattern[] = { 0x03, 0xC6, 0x8F, 0xE2, 0x18, 0xCA, 0x8C, 0xE2, 0x94, 0xFD, 0xBC, 0xE5, 0x03, 0xC6, 0x8F, 0xE2 };

My main problem is what i need like each 0x03 in one byte cell, of the output array exactly as i described,

if i use this

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

it doesn't actually do what i need because when i debug it , i look at the bytes vector and i see it puts 0 in a cell, x in a cell , 0 , in cell , 3 in cell , which is not what i want, is there anyway to solve this kind of problem ? the output aint like it should be, I added what the output should be there in the code something like this:

        /*
     * output should be:
      0x03
      0xC6
      0x8F
     * etc
     * .
     * .
     */

the somePtr array is my last output should be, with as i described up.

thanks.

Aucun commentaire:

Enregistrer un commentaire