将十六进制字符串转换为字节数组

将十六进制字符串转换为字节数组

转换可变长度十六进制字符串的最佳方法是什么?"01A1"到包含该数据的字节数组。

即转换为:

std::string = "01A1";

变成这样

char* hexArray;int hexLength;

或者这个

std::vector<char> hexArray;

所以当我把这个写到一个文件hexdump -C我得到的二进制数据包含01A1.


翻翻过去那场雪
浏览 346回答 4
4回答

哈士奇WWW

所以为了好玩,我很好奇我是否能在编译时完成这种转换。它没有太多的错误检查,并且是在VS 2015中完成的,它还不支持C+14 Conexpr函数(这就是HexCharToInt的样子)。它采用c-字符串数组,将一对字符转换为单个字节,并将这些字节展开为一个统一的初始化列表,用于初始化作为模板参数提供的T类型。T可以替换为像std:Array这样的东西来自动返回数组。#include&nbsp;<cstdint>#include&nbsp;<initializer_list>#include&nbsp;<stdexcept>#include&nbsp;<utility>/*&nbsp;Quick&nbsp;and&nbsp;dirty&nbsp;conversion&nbsp;from&nbsp;a&nbsp;single&nbsp;character&nbsp;to&nbsp;its&nbsp;hex&nbsp;equivelent&nbsp;*/constexpr&nbsp;std::uint8_t&nbsp;HexCharToInt(char&nbsp;Input){ &nbsp;&nbsp;&nbsp;&nbsp;return &nbsp;&nbsp;&nbsp;&nbsp;((Input&nbsp;>=&nbsp;'a')&nbsp;&&&nbsp;(Input&nbsp;<=&nbsp;'f')) &nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;(Input&nbsp;-&nbsp;87) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;((Input&nbsp;>=&nbsp;'A')&nbsp;&&&nbsp;(Input&nbsp;<=&nbsp;'F')) &nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;(Input&nbsp;-&nbsp;55) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;((Input&nbsp;>=&nbsp;'0')&nbsp;&&&nbsp;(Input&nbsp;<=&nbsp;'9')) &nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;(Input&nbsp;-&nbsp;48) &nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;throw&nbsp;std::exception{};}/*&nbsp;Position&nbsp;the&nbsp;characters&nbsp;into&nbsp;the&nbsp;appropriate&nbsp;nibble&nbsp;*/constexpr&nbsp;std::uint8_t&nbsp;HexChar(char&nbsp;High,&nbsp;char&nbsp;Low){ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(HexCharToInt(High)&nbsp;<<&nbsp;4)&nbsp;|&nbsp;(HexCharToInt(Low));}/*&nbsp;Adapter&nbsp;that&nbsp;performs&nbsp;sets&nbsp;of&nbsp;2&nbsp;characters&nbsp;into&nbsp;a&nbsp;single&nbsp;byte&nbsp;and&nbsp;combine&nbsp;the&nbsp;results&nbsp;into&nbsp;a&nbsp;uniform&nbsp;initialization&nbsp;list&nbsp;used&nbsp;to&nbsp;initialize&nbsp;T&nbsp;*/template&nbsp;<typename&nbsp;T,&nbsp;std::size_t&nbsp;Length,&nbsp;std::size_t&nbsp;...&nbsp;Index>constexpr&nbsp;T&nbsp;HexString(const&nbsp;char&nbsp;(&Input)[Length],&nbsp;const&nbsp;std::index_sequence<Index...>&){ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;T{HexChar(Input[(Index&nbsp;*&nbsp;2)],&nbsp;Input[((Index&nbsp;*&nbsp;2)&nbsp;+&nbsp;1)])...};}/*&nbsp;Entry&nbsp;function&nbsp;*/template&nbsp;<typename&nbsp;T,&nbsp;std::size_t&nbsp;Length>constexpr&nbsp;T&nbsp;HexString(const&nbsp;char&nbsp;(&Input)[Length]){ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;HexString<T>(Input,&nbsp;std::make_index_sequence<(Length&nbsp;/&nbsp;2)>{});}constexpr&nbsp;auto&nbsp;Y&nbsp;=&nbsp;KS::Utility::HexString<std::array<std::uint8_t,&nbsp;3>>("ABCDEF");

蝴蝶刀刀

如果您想使用OpenSSL来完成它,我发现了一个巧妙的技巧:BIGNUM&nbsp;*input&nbsp;=&nbsp;BN_new();int&nbsp;input_length&nbsp;=&nbsp;BN_hex2bn(&input,&nbsp;argv[2]);input_length&nbsp;=&nbsp;(input_length&nbsp;+&nbsp;1)&nbsp;/&nbsp;2;&nbsp;//&nbsp;BN_hex2bn()&nbsp;returns&nbsp;number&nbsp;of&nbsp;hex&nbsp;digitsunsigned&nbsp;char&nbsp;*input_buffer&nbsp;=&nbsp;(unsigned&nbsp;char*)malloc(input_length);retval&nbsp;=&nbsp;BN_bn2bin(input,&nbsp;input_buffer);只需确保去掉任何引导‘0x’到字符串。
打开App,查看更多内容
随时随地看视频慕课网APP