Is it possible to make lexical_cast function to support stirngs wtih
'0x' format.
e.g
boost::lexical_cast<unsigned char>( "0x7E" )
This formatting throws bad_lexical_cast exception. Is there any way to
do that?
Boost would probably be the correct place to ask this question.
However, lexical_cast<> is defined to have stream conversion semantics.
Sadly, streams don't understand the "0x" notation. That is,
std::istringstream in("0x7e");
int i;
in >> i;
results in i have 0 as its value;
The second problem is that unsigned char is treated as a character and
not as an integral type. Thus:
std::istringstream in("17");
unsigned char c;
in >> c;
results in c having a value of '1' or 49 if treated as an int. I am
sure that it could be done if they wanted to feed the string into strtol
() then convert it to a char, but does that make more sense than what
they do currently? It might for you, but for everyone?
The first step is probably convert to an unsigned short and then to an
unsigned char. That would at least get the conversion to a number going
rather than just grabbing the first character. I'm not real sure how
you would convince the stream lexical_cast<> uses to convert from hex.
If you were using a real stream, you could do:
std::istringstream in("7f");
unsigned short us;
in >> std::hex >> us;
Like I said at the beginning, you might want to post to the boost group
to see if they have a good answer.
HTH,
joe