Boost lexical_cast and hexa decimal format

Discussion in 'C++' started by Sarath, Jan 29, 2008.

  1. Sarath

    Sarath Guest

    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?
    Sarath, Jan 29, 2008
    #1
    1. Advertising

  2. Sarath

    Joe Greer Guest

    Sarath <> wrote in news:4c975578-3afe-4c83-a0a3-
    :

    > 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
    Joe Greer, Jan 29, 2008
    #2
    1. Advertising

  3. 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.
    Phil Endecott, Jan 29, 2008
    #3
  4. Sarath

    James Kanze Guest

    On Jan 29, 4:57 pm, Joe Greer <> wrote:
    > Sarath <> wrote in news:4c975578-3afe-4c83-a0a3-
    > :


    > > 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:
    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
    James Kanze, Jan 30, 2008
    #4
  5. Sarath

    Joe Greer Guest

    James Kanze <> wrote in news:791a234b-5a67-4e07-
    :

    >> 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
    Joe Greer, Jan 30, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PC
    Replies:
    2
    Views:
    3,925
    Marc Guardiani
    Nov 12, 2003
  2. Golan

    Converting hexa to decimal

    Golan, Jan 8, 2004, in forum: C Programming
    Replies:
    3
    Views:
    2,150
    Kevin Goodsell
    Jan 9, 2004
  3. Sarath

    using lexical_cast

    Sarath, Aug 31, 2007, in forum: C++
    Replies:
    5
    Views:
    342
    Cholo Lennon
    Aug 31, 2007
  4. Alex Vinokur
    Replies:
    4
    Views:
    544
    James Kanze
    Mar 3, 2008
  5. Rui Maciel

    C++0x: lexical_cast?

    Rui Maciel, May 6, 2011, in forum: C++
    Replies:
    4
    Views:
    2,243
    Noah Roberts
    May 6, 2011
Loading...

Share This Page