2

私の入力は

char *str = "/send 13 01 09 00";

出力が必要です

BYTE* result = { 0x13, 0x09, 0x00 };

(/sendをスキップします)

誰かが16進バイトの文字列からバイトを取得する解決策を提供できますか?

これは私が試したことです:

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string>

byte *ToPacket(const char* str)
{
    const char *pos = str;
    unsigned char val[sizeof(str)/sizeof(str[0])];

    size_t count = 0;

    for(count = 0; count < sizeof(val)/sizeof(val[0]); count++)
    {
        sscanf_s(pos, "%2hhx", &val[count]);
        pos += 2 * sizeof(char);
    }

    return val;
}

int _tmain(int argc, _TCHAR* argv[])
{

redo:

    while (true)
    {
        std::string key;
        std::getline(std::cin, key); 

        if (key != "")
        {
            if (key == "/hit")
            {
                BYTE packet[] = { 0x13, 0x01, 0x00 };
                int size = sizeof(packet) / sizeof(packet[0]);              

                std::cout << "[FatBoy][" << key << "]: Hit\n";
            }
            else if (strstr(key.c_str(), "/send"))
            {
                BYTE * packet = ToPacket(key.c_str());
                int size = sizeof(packet) / sizeof(packet[0]);

            }


            key = "";
            break;
        }

        Sleep(100);
    }
    goto redo; 
}
4

2 に答える 2

2
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

std::string s("/send 13 01 09 00");
int v1,v2,v3,v4;
std::string cmd;
std::istringstream inp_stream(s);
inp_stream >> cmd >> std::setbase(16) >> v1 >> v2 >> v3 >> v4;
于 2012-12-18T22:32:14.850 に答える
2

IO マニピュレータstd::istringstreamと一緒に使用して、以下を設定します。std::hexstd::vector<unsigned char>

std::string s("13 01 09 00");
std::vector<unsigned char> v;
std::istringstream in(s);
in >> std::hex;

unsigned short c;
while (in >> c) v.push_back(static_cast<unsigned char>(c));

http://ideone.com/HTJmzJでデモを参照してください。

于 2012-12-18T22:33:54.327 に答える