|
Boost : |
Subject: Re: [boost] hex/unhex()?
From: michi7x7 (michi_at_[hidden])
Date: 2011-11-02 16:32:14
Hi,
>>> As Marshal pointed out, Spirit supports that easily
>
> Actually, please disregard what I wrote, it is complete nonsense. I misread
> your initial mail.
>
> Note to self: drink coffee before responding to email!
>
> Regards Hartmut
Actually, it's not really difficult.
hex() is done, unhex() is up to someone else, cause I've got to go:
#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
namespace hex_cast
{
template<class T>
std::string hex(T const& input)
{
typedef typename T::const_iterator iterator_t;
std::string temp;
temp.reserve(input.size() * sizeof(typename T::value_type) * 2);
iterator_t it(input.begin()), end(input.end());
using boost::spirit::karma::generate;
using boost::spirit::karma::hex;
for(; it != end; ++it)
generate(std::back_inserter(temp), hex, *it);
return temp;
}
}
int main()
{
std::string s("abode");
std::cout << hex_cast::hex(s) << std::endl;
return 0;
}
HTH
michi7x7
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk