Converting a hex string to a byte array

后端 未结 19 1954
时光取名叫无心
时光取名叫无心 2020-11-22 12:10

What is the best way to convert a variable length hex string e.g. \"01A1\" to a byte array containing that data.

i.e converting this:

st         


        
19条回答
  •  逝去的感伤
    2020-11-22 12:48

    C++11 variant (with gcc 4.7 - little endian format):

        #include 
        #include 
    
        std::vector decodeHex(const std::string & source)
        {
            if ( std::string::npos != source.find_first_not_of("0123456789ABCDEFabcdef") )
            {
                // you can throw exception here
                return {};
            }
    
            union
            {
                uint64_t binary;
                char byte[8];
            } value{};
    
            auto size = source.size(), offset = (size % 16);
            std::vector binary{};
            binary.reserve((size + 1) / 2);
    
            if ( offset )
            {
                value.binary = std::stoull(source.substr(0, offset), nullptr, 16);
    
                for ( auto index = (offset + 1) / 2; index--; )
                {
                    binary.emplace_back(value.byte[index]);
                }
            }
    
            for ( ; offset < size; offset += 16 )
            {
                value.binary = std::stoull(source.substr(offset, 16), nullptr, 16);
                for ( auto index = 8; index--; )
                {
                    binary.emplace_back(value.byte[index]);
                }
            }
    
            return binary;
        }
    

    Crypto++ variant (with gcc 4.7):

    #include 
    #include 
    
    #include 
    #include 
    
    std::vector decodeHex(const std::string & source)
    {
        std::string hexCode;
        CryptoPP::StringSource(
                  source, true,
                  new CryptoPP::HexDecoder(new CryptoPP::StringSink(hexCode)));
    
        return std::vector(hexCode.begin(), hexCode.end());
    }
    

    Note that the first variant is about two times faster than the second one and at the same time works with odd and even number of nibbles (the result of "a56ac" is {0x0a, 0x56, 0xac}). Crypto++ discards the last one if there are odd number of nibbels (the result of "a56ac" is {0xa5, 0x6a}) and silently skips invalid hex characters (the result of "a5sac" is {0xa5, 0xac}).

提交回复
热议问题