468,513 Members | 1,686 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Boost lexical_cast and hexa decimal format

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?
Jan 29 '08 #1
2 5358
Sarath <CS*****@gmail.comwrote in news:4c975578-3afe-4c83-a0a3-
68**********@v29g2000hsf.googlegroups.com:
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
Jan 29 '08 #2
Sarath wrote:
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.
Jan 29 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Golan | last post: by
2 posts views Thread by Nadav | last post: by
1 post views Thread by Chandra | last post: by
1 post views Thread by Noah Roberts | last post: by
3 posts views Thread by neino | last post: by
7 posts views Thread by BubbaT | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.