Boost logo

Boost :

Subject: Re: [boost] hex/unhex()?
From: Hartmut Kaiser (hartmut.kaiser_at_[hidden])
Date: 2011-11-02 07:32:58


> On Wed, Nov 2, 2011 at 7:52 AM, Sergey Cheban <s.cheban_at_[hidden]> wrote:
> > 02.11.2011 0:09, Olaf van der Spek пишет:
> >
> >> Does Boost have hex/unhex() that support std::string?
> >> I can't find them and I think they'd be quite handy to have.
> >
> > How exactly should it interpret the following lines:
> > 01 02
> > 0102
> > 01 02
> > 010 2
> > 012
> > 01 2
> > 01\n02
> > 01 | 02
> > etc.
>
> Good question. I'd be happy with one that only accepts the simplest
> variant.
>
> > For the boost library, we need the universal parser that can be
> > customized by the policies. It is also reasonable to have the
> > range-based, stream-based, 0-terminated and wchar_t-based versions.
> >
> > But it is much easier to write exactly what you need in your code.
>
> Not really. Copy/paste development is bad. ;)

As Marshal pointed out, Spirit supports that easily:

    std::string input("61626f6465");
    std::string str;
    if (qi::parse(input.begin(), input.end(), qi::hex, str))
        assert(str == "abode");

and

    std::string str("abode");
    std::string output;
    if (karma::generate(back_inserter(output), karma::hex, str))
        assert(output == "61626f6465");

If you want to avoid the cost of possible reallocations in output, just call output.reserve(...) upfront. Otherwise both code snippets generate code equivalent to hand written assembly and have been shown to be as fast as it can get.

HTH
Regards Hartmut
---------------
http://boost-spirit.com
http://stellar.cct.lsu.edu


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk