Re: Hex to ascii hex
goodTweetieBird wrote:
Is there a lib routine for converting an arbitrary number of bytes to
their ascii representation or must one do it byte by byte? I receive
known quantities of bytes from a pipe and need to display them as hex
for debugging purposes.
#include <string>
#include <algorithm>
struct AppendHexTo {
std::string & towhat;
AppendHexTo(std::string& w) : towhat(w) {}
void operator()(unsigned char b) {
static char const hexdigit[] = "0123456789ABCDEF";
towhat += hexdigit[(b & 0xF0) >> 4];
towhat += hexdigit[b & 0x0F];
}
};
#include <iostream>
#include <ostream>
int main()
{
unsigned char buffer[] = { 1,2,3,4,5,6,7,8,9,10,11,12,
100,101,102,103,104,105,106 };
std::string result;
std::for_each(buffer, buffer + sizeof(buffer), AppendHexTo(result));
std::cout << result << std::endl;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask