istringstream "hexadecimal string" value

Discussion in 'C++' started by ma740988, Oct 27, 2004.

  1. ma740988

    ma740988 Guest

    The string object value_f doesn't produce the right output. At issue,
    - I suspect - is the conversion from string to int with istringstream.
    An alternate approach? Thanks in advance

    #include <iostream>
    #include <sstream>
    #include <string>

    using namespace std;

    // Conversion Functions
    template<typename T>
    T fromString(const std::string& s) {
    std::istringstream is(s);
    T t;
    is >> t;
    return t;
    }
    template<typename T>
    std::string toString(const T& t) {
    std::eek:stringstream s;
    s << t;
    return s.str();
    }


    //
    // test function to convert values:
    // 0xAFFF from string to int
    // 0xAFFF from int to string

    int main()
    {

    int d(0xAFFF);
    string value_d = toString( d );
    cout << "value_d is [" << value_d << "]" << endl;

    string value_e( " 4999 " );
    string value_f( " 0xAFFF " );

    int val_1 = fromString<int>( value_e );
    cout << "val_1 is [" << val_1 << "]" << endl;
    int val_2 = fromString<int>( value_f );
    cout << "val_2 is [" << val_2 << "]" << std::hex << endl;

    }
     
    ma740988, Oct 27, 2004
    #1
    1. Advertising

  2. "ma740988" <> wrote...
    > The string object value_f doesn't produce the right output. At issue,
    > - I suspect - is the conversion from string to int with istringstream.
    > An alternate approach?


    The default extraction operator with default format setting expects
    a _decimal_ notation.

    > Thanks in advance
    >
    > #include <iostream>
    > #include <sstream>
    > #include <string>
    >
    > using namespace std;
    >
    > // Conversion Functions
    > template<typename T>
    > T fromString(const std::string& s) {


    Change to

    T fromString(const std::string& s,
    std::ios_base& (*f)(std::ios_base&) = std::dec) {


    > std::istringstream is(s);
    > T t;
    > is >> t;


    Change to

    is >> f >> t;

    > return t;
    > }
    > template<typename T>
    > std::string toString(const T& t) {


    Change to

    std::string toString(const T& t,
    std::ios_base& (*f)(std::ios_base&) = std::dec) {

    > std::eek:stringstream s;
    > s << t;


    Change to

    s << f << t;

    > return s.str();
    > }
    >
    >
    > //
    > // test function to convert values:
    > // 0xAFFF from string to int
    > // 0xAFFF from int to string
    >
    > int main()
    > {
    >
    > int d(0xAFFF);
    > string value_d = toString( d );
    > cout << "value_d is [" << value_d << "]" << endl;
    >
    > string value_e( " 4999 " );
    > string value_f( " 0xAFFF " );
    >
    > int val_1 = fromString<int>( value_e );
    > cout << "val_1 is [" << val_1 << "]" << endl;
    > int val_2 = fromString<int>( value_f );


    Change to

    int val_e = fromString<int>( value_f, hex );

    > cout << "val_2 is [" << val_2 << "]" << std::hex << endl;
    >
    > }


    HTH

    V
     
    Victor Bazarov, Oct 27, 2004
    #2
    1. Advertising

  3. ma740988

    ma740988 Guest

    "Victor Bazarov" <> wrote in message news:<ZSDfd.253989$wV.7201@attbi_s54>...
    > "ma740988" <> wrote...


    > > int main()
    > > {
    > >
    > > int d(0xAFFF);
    > > string value_d = toString( d );
    > > cout << "value_d is [" << value_d << "]" << endl;
    > >
    > > string value_e( " 4999 " );
    > > string value_f( " 0xAFFF " );
    > >
    > > int val_1 = fromString<int>( value_e );
    > > cout << "val_1 is [" << val_1 << "]" << endl;
    > > int val_2 = fromString<int>( value_f );

    >
    > Change to
    >
    > int val_e = fromString<int>( value_f, hex );
    >
    > > cout << "val_2 is [" << val_2 << "]" << std::hex << endl;
    > >
    > > }

    >
    > HTH
    >
    > V

    Beautiful. One question though (Haven't gotten through Langer/Kreft)
    oct doesn't co-operate

    template<typename T>
    T fromString(const std::string& s,
    std::ios_base& (*f)(std::ios_base&) = std::dec)
    {
    std::istringstream is(s);
    T t;
    is >> f >> t;
    return t;
    }

    string value_e( " 4999 " );
    int val_e = fromString<int>( value_e, oct);
    The output is 4.
     
    ma740988, Oct 27, 2004
    #3
  4. ma740988 wrote:
    > [..] One question though (Haven't gotten through Langer/Kreft)
    > oct doesn't co-operate
    >
    > template<typename T>
    > T fromString(const std::string& s,
    > std::ios_base& (*f)(std::ios_base&) = std::dec)
    > {
    > std::istringstream is(s);
    > T t;
    > is >> f >> t;
    > return t;
    > }
    >
    > string value_e( " 4999 " );
    > int val_e = fromString<int>( value_e, oct);
    > The output is 4.


    '9' is not a valid input for the octal converter, is it?

    V
     
    Victor Bazarov, Oct 27, 2004
    #4
  5. ma740988

    ma740988 Guest

    Victor Bazarov <> wrote in message news:<i5Ofd.7225$09.us.to.verio.net>...
    > ma740988 wrote:
    > > [..] One question though (Haven't gotten through Langer/Kreft)
    > > oct doesn't co-operate
    > >
    > > template<typename T>
    > > T fromString(const std::string& s,
    > > std::ios_base& (*f)(std::ios_base&) = std::dec)
    > > {
    > > std::istringstream is(s);
    > > T t;
    > > is >> f >> t;
    > > return t;
    > > }
    > >
    > > string value_e( " 4999 " );
    > > int val_e = fromString<int>( value_e, oct);
    > > The output is 4.

    >
    > '9' is not a valid input for the octal converter, is it?
    >
    > V


    Ah!!! Being 'wrapped up in the stream' , I overlooked that, however it
    gets interesting. First, I'm assuming that the stream modifier
    std::eek:ct is what you're referring to as the octal converter? Now i
    need to grab my text for a refresher but I conclude - based on your
    reply - that the conversion is predicated upon passing single digit to
    the converter. IOW 4 is passed to the converter, then 9 and so on.

    Now, heres a case where the 'octal converter/stream modifier(?)'
    produced the desired output which refutes my prior conclusion.

    int idx(4999);
    cout << oct << idx << endl;
     
    ma740988, Oct 27, 2004
    #5
  6. ma740988 wrote:
    > Victor Bazarov <> wrote in message news:<i5Ofd.7225$09.us.to.verio.net>...
    >
    >>ma740988 wrote:
    >>
    >>>[..] One question though (Haven't gotten through Langer/Kreft)
    >>>oct doesn't co-operate
    >>>
    >>>template<typename T>
    >>>T fromString(const std::string& s,
    >>> std::ios_base& (*f)(std::ios_base&) = std::dec)
    >>>{
    >>> std::istringstream is(s);
    >>> T t;
    >>> is >> f >> t;
    >>> return t;
    >>>}
    >>>
    >>>string value_e( " 4999 " );
    >>>int val_e = fromString<int>( value_e, oct);
    >>>The output is 4.

    >>
    >>'9' is not a valid input for the octal converter, is it?
    >>
    >>V

    >
    >
    > Ah!!! Being 'wrapped up in the stream' , I overlooked that, however it
    > gets interesting. First, I'm assuming that the stream modifier
    > std::eek:ct is what you're referring to as the octal converter? Now i
    > need to grab my text for a refresher but I conclude - based on your
    > reply - that the conversion is predicated upon passing single digit to
    > the converter. IOW 4 is passed to the converter, then 9 and so on.
    >
    > Now, heres a case where the 'octal converter/stream modifier(?)'
    > produced the desired output which refutes my prior conclusion.
    >
    > int idx(4999);
    > cout << oct << idx << endl;


    'oct', like 'dec' or 'hex', control how the string is converted to and
    from the internal representation. Those format flags are bi-directional.

    You can think of it that they turn on certain converters in the stream.
    Once you say 'oct', the next string is interpreted as octal if you input
    or the next number is output in the octal form if you output. Converters
    are bi-directional too.

    V
     
    Victor Bazarov, Oct 28, 2004
    #6
  7. ma740988

    ma740988 Guest

    Victor Bazarov <> wrote in message news:<i5Ofd.7225
    >



    I'm using google as a newsreader (too cheap right now) and as a result
    I dont see your responses momentarily.
    In any event, after seeing your use of the fuction pointer that
    returns a reference to ios_base. I went back and retrofitted some
    'old' code to do the same.
    One things that's puzzled me in the past (string related) is the
    results of the program below. The output is 0 when i expect 0x00.
    0xFF, etc. works fine.


    //PARAMETERIZED OVER CHARACTER AND NUMERIC TYPE TO BE CONVERTED
    template <typename CT, typename T>
    std::basic_string<CT> convertToStr(const T& t, std::ios_base &
    (*f)(std::ios_base&), const std::streamsize precision)
    {
    std::basic_ostringstream<CT> oss;
    oss.setf(std::ios::showbase);
    oss << std::fixed << std::setprecision(precision)<< f << t;
    return oss.str();
    };

    int main()
    {
    cout << convertToStr<char>(0x00, std::hex, 0) << endl;
    }
     
    ma740988, Oct 28, 2004
    #7
  8. ma740988 wrote:
    > Victor Bazarov <> wrote in message news:<i5Ofd.7225
    >
    >
    >
    > I'm using google as a newsreader (too cheap right now) and as a result
    > I dont see your responses momentarily.
    > In any event, after seeing your use of the fuction pointer that
    > returns a reference to ios_base. I went back and retrofitted some
    > 'old' code to do the same.
    > One things that's puzzled me in the past (string related) is the
    > results of the program below. The output is 0 when i expect 0x00.
    > 0xFF, etc. works fine.


    I am not surprised. The conversion to hex is done precisely as if it
    were done by 'printf'. Since 'printf' never prepends 0 with 0x, neither
    does the ostream. The C Standard says that only a non-zero value will
    be prefixed with 0x. If you want _all_ values to be prefixed, you should
    (a) drop the 'showbase' and (b) add "0x" for the hex.

    >
    >
    > //PARAMETERIZED OVER CHARACTER AND NUMERIC TYPE TO BE CONVERTED
    > template <typename CT, typename T>
    > std::basic_string<CT> convertToStr(const T& t, std::ios_base &
    > (*f)(std::ios_base&), const std::streamsize precision)
    > {
    > std::basic_ostringstream<CT> oss;
    > oss.setf(std::ios::showbase);
    > oss << std::fixed << std::setprecision(precision)<< f << t;
    > return oss.str();
    > };
    >
    > int main()
    > {
    > cout << convertToStr<char>(0x00, std::hex, 0) << endl;
    > }



    V
     
    Victor Bazarov, Oct 28, 2004
    #8
    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. Samuele Armondi

    Allocating istringstream objects

    Samuele Armondi, Jul 11, 2003, in forum: C++
    Replies:
    1
    Views:
    379
    Samuele Armondi
    Jul 11, 2003
  2. Agent Mulder

    istringstream to bool

    Agent Mulder, Aug 23, 2003, in forum: C++
    Replies:
    8
    Views:
    1,051
    Agent Mulder
    Aug 24, 2003
  3. Viet Le Hong

    istringstream question

    Viet Le Hong, Sep 18, 2003, in forum: C++
    Replies:
    3
    Views:
    686
    Buster Copley
    Sep 18, 2003
  4. bml

    istringstream???

    bml, Jan 27, 2004, in forum: C++
    Replies:
    3
    Views:
    1,065
    Karl Heinz Buchegger
    Jan 27, 2004
  5. Donald Canton

    istringstream syntax error question

    Donald Canton, Jan 29, 2004, in forum: C++
    Replies:
    1
    Views:
    411
    David Harmon
    Jan 30, 2004
Loading...

Share This Page