Boost lexical_cast and hexa decimal format

S

Sarath

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?
 
J

Joe Greer

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
 
P

Phil Endecott

Sarath said:
Is it possible to make lexical_cast function to support stirngs wtih
'0x' format.

I believe that I have seen this question asked on the Boost list. Try
searching their archive. I believe that the answer is "no".

Phil.
 
J

James Kanze

(e-mail address removed):
Boost would probably be the correct place to ask this question.

One might also ask if lexical_cast<> is the correct tool for
this. It sounds more like a parsing problem, and not a type
conversion, so a priori, anything whose name ends in _cast is
not the right tool.
However, lexical_cast<> is defined to have stream conversion
semantics. Sadly, streams don't understand the "0x" notation.

Since when? It works in all of the implementations I have
access to, and is specified by the standard.
std::istringstream in("0x7e");
int i;
in >> i;
results in i have 0 as its value;

Yes. I don't know why the standard committee changed this from
the classical iostream specification, but in standard iostream,
the basefield is initialized to std::ios::dec, rather than to 0,
as was the case in the classical iostream. Set the basefield to
0, and it should work. (Because of type considerations, you
should probably write:
in.setf( std::ios::fmtflags(), std::ios::basefield ) ;
to be portable.)
The second problem is that unsigned char is treated as a
character and not as an integral type.

One could argue that that's an error in lexical_cast<>. I would
certainly expect:

int i = 0x7E ;
unsigned char uc
= boost::lexical_cast< unsigned char >( i ) ;

to initialize uc with 0x7E.

Of course, in that case, you probably wouldn't use lexical_cast.
(But then, I've never found a case where you would. To me,
lexical_cast looks like a solution in search of a problem.)
 
J

Joe Greer

Since when? It works in all of the implementations I have
access to, and is specified by the standard.

Well, I should have said "By default streams don't understand (or maybe
honor is a better word) the "0x" notation then. :) I suppose the OP
could edit their copy of lexical_cast to set basefield to 0 for the
stringstream it uses and it would then pay attention to the 0x.
However, that can cause problems when it's time to update the boost
library.
One could argue that that's an error in lexical_cast<>. I would
certainly expect:

int i = 0x7E ;
unsigned char uc
= boost::lexical_cast< unsigned char >( i ) ;

to initialize uc with 0x7E.

And that would probably work (I don't know for sure). It's the
converting from the string "0x7e" that doesn't work. lexical_cast<> was
primarily designed to go to and from strings and uses a stringstream in
it's implementation. To use it as a normal cast would be quite
inefficient.

joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top