开发者

Convert Hexdump to string in c

Is there a way to convert a hexdump e.g. aec4d2f3c6a4e70ea6cea074f65812d2a34b180cc92b817e开发者_如何学Godcd867167e7a91c5beb942f0 to a string in c so that every two hexadecimal digits make a char? If so, what?


Reads from stdin and prints to stdout:

int main() 
{
    int ch;
    while(scanf("%2x", &ch) == 1)
        putchar(ch);
}

I think you can modify it easily yourself for your specific source and destination requirements.


One of the ways:

size_t unBytes = strHex.size() / 2;
std::string strResult(unBytes, 0);
for (size_t i = 0; i < unBytes; ++i)
{
    std::istringstream in(strHex.substr(2*i, 2));
    int byte = 0;
    in >> std::hex >> byte;
    strResult[i] = byte;
}


Use scanf with %hhx repeatedly. See man scanf.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜