十六进制字符串到BYTE c ++

我正在尝试将

形式的十六进制字符串转换为
std::string mystring = "00A4040C06FF5142534014";

以以下形式发送给BYTE:

BYTE cmd2[] = {0x00,0xA4,0x04,0x0C,0x06,0xFF,0x51,0x42,0x53,0x40,0x14};

我尝试使用以下功能:

std::vector<char> HexToBytes(const std::string& hex)
{
    std::vector<char> bytes;
    for (unsigned int i = 0; i < hex.length(); i += 2) {
        std::string byteString = hex.substr(i,2);
        char byte = (char) strtol(byteString.c_str(),NULL,16);
        bytes.push_back(byte);
    }

    return bytes;
}

但这并没有给我正确的转换。也许有人可以帮助我?

hhhhj2006 回答:十六进制字符串到BYTE c ++

没有从std::vector<char>const unsigned char*的隐式转换。

std::vector<BYTE> HexToBytes(const std::string& hex)
{
    std::vector<BYTE> bytes;
...
auto cmd = HexToBytes(mystring);
rv = SCardTransmit(hCard,&pioSendPci,&cmd[0],cmd.size(),NULL,pbRecvBuffer,&dwRecvLength);
本文链接:https://www.f2er.com/3022125.html

大家都在问