我的输入是
char *str = "/send 13 01 09 00";
我需要输出
BYTE* result = { 0x13, 0x09, 0x00 };
(所以跳过/发送)
有人可以为我提供一个从一串十六进制字节中获取字节的解决方案吗?
这是我尝试过的:
#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;
}