From: goodTweetieBird on

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.

thanx,

gtb
From: Victor Bazarov on
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
From: goodTweetieBird on
On Jun 20, 10:22 am, Victor Bazarov <v.Abaza...(a)comAcast.net> wrote:
> 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

Thanks!
From: Vladimir Grigoriev on

"Victor Bazarov" <v.Abazarov(a)comAcast.net> wrote in message
news:g3geer$fdc$1(a)news.datemas.de...
> goodTweetieBird wrote:
> #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];

It would be simpler to write
towhat += hexdigit[b>> 4];

> towhat += hexdigit[b & 0x0F];
> }
> };

Vladimir Grigoriev


From: Giovanni Dicanio on

"goodTweetieBird" <goodTweetieBird(a)hotmail.com> ha scritto nel messaggio
news:1a80fd9f-992b-47f2-b0a7-9794eaee5f1b(a)c58g2000hsc.googlegroups.com...

> struct AppendHexTo {
> std::string & towhat;
> AppendHexTo(std::string& w) : towhat(w) {}

I think 'w' can be passed as const reference here:

AppendHexTo( const std::string & w ) ...

Giovanni