Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Boost lexical_cast and hexa decimal format

Reply
Thread Tools

Boost lexical_cast and hexa decimal format

 
 
Sarath
Guest
Posts: n/a
 
      01-29-2008
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?
 
Reply With Quote
 
 
 
 
Joe Greer
Guest
Posts: n/a
 
      01-29-2008
Sarath <(E-Mail Removed)> wrote in news:4c975578-3afe-4c83-a0a3-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> 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
 
Reply With Quote
 
 
 
 
Phil Endecott
Guest
Posts: n/a
 
      01-29-2008
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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      01-30-2008
On Jan 29, 4:57 pm, Joe Greer <(E-Mail Removed)> wrote:
> Sarath <(E-Mail Removed)> wrote in news:4c975578-3afe-4c83-a0a3-
> (E-Mail Removed):


> > 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.


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.

> That is,


> 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.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Joe Greer
Guest
Posts: n/a
 
      01-30-2008
James Kanze <(E-Mail Removed)> wrote in news:791a234b-5a67-4e07-
(E-Mail Removed):

>> 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.


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.

>
>> 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.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use boost::lexical_cast function in Gcc compiler[Linux] suse Software 0 03-06-2009 06:35 AM
Is boost::lexical_cast<>() always bijective? Alex Vinokur C++ 4 03-03-2008 01:51 PM
using lexical_cast Sarath C++ 5 08-31-2007 07:59 PM
Converting hexa to decimal Golan C Programming 3 01-09-2004 09:03 AM
hexa bus to decimal 7 segments - VHDL... PC VHDL 2 11-12-2003 03:43 AM



Advertisments