How to write data to a ostringstream?

Discussion in 'C++' started by Angus, Jan 13, 2007.

  1. Angus

    Angus Guest

    Hello

    I am using ostringstream and want to write some ascii characters (not all
    alphanumeric) to the stream.

    I can do this sort of thing:

    int value = 9;
    oss << value;

    and that works fine.

    But I have some ascii values as hex - eg 0xa1. I can't do this:

    oss << 0xa1;

    Well, I can but I just get the decimal number of the value added. I just
    want to append the ascii value to the string - it is a funny i sort of
    character in this example.

    Do I need to convert the 0xa1 into the relevant ascii symbol and then append
    that? If so, how to convert? Is there a ostringstream function which will
    do the conversion for me?

    Angus
     
    Angus, Jan 13, 2007
    #1
    1. Advertising

  2. Angus

    Alan Johnson Guest

    Angus wrote:
    > Hello
    >
    > I am using ostringstream and want to write some ascii characters (not all
    > alphanumeric) to the stream.
    >
    > I can do this sort of thing:
    >
    > int value = 9;
    > oss << value;
    >
    > and that works fine.
    >
    > But I have some ascii values as hex - eg 0xa1. I can't do this:
    >
    > oss << 0xa1;
    >
    > Well, I can but I just get the decimal number of the value added. I just
    > want to append the ascii value to the string - it is a funny i sort of
    > character in this example.
    >
    > Do I need to convert the 0xa1 into the relevant ascii symbol and then append
    > that? If so, how to convert? Is there a ostringstream function which will
    > do the conversion for me?
    >
    > Angus
    >
    >


    Insert std::hex into the stream (defined in <iomanip>). If you want it
    to show the "0x" for you as well, also insert std::showbase.

    oss << std::hex << std::showbase << 0xa ;

    --
    Alan Johnson
     
    Alan Johnson, Jan 13, 2007
    #2
    1. Advertising

  3. Angus wrote:

    > But I have some ascii values as hex - eg 0xa1. I can't do this:
    > oss << 0xa1;
    > Well, I can but I just get the decimal number of the value added. I just
    > want to append the ascii value to the string - it is a funny i sort of
    > character in this example.


    0xa1 is not an ascii value, the ascii charset has only codes in the 0-127
    range. I suppose you mean some ascii extension.

    If what you want is to write the character with that code, use a character
    literal: '\xA1'.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 13, 2007
    #3
  4. Angus

    Jim Langston Guest

    "Angus" <> wrote in message
    news:eobloo$o5r$1$...
    > Hello
    >
    > I am using ostringstream and want to write some ascii characters (not all
    > alphanumeric) to the stream.
    >
    > I can do this sort of thing:
    >
    > int value = 9;
    > oss << value;
    >
    > and that works fine.
    >
    > But I have some ascii values as hex - eg 0xa1. I can't do this:
    >
    > oss << 0xa1;


    0xa1 is a number.
    '0xa1' is a character.

    is
    oss << '0xa1';
    what you want?

    >
    > Well, I can but I just get the decimal number of the value added. I just
    > want to append the ascii value to the string - it is a funny i sort of
    > character in this example.
    >
    > Do I need to convert the 0xa1 into the relevant ascii symbol and then
    > append
    > that? If so, how to convert? Is there a ostringstream function which
    > will
    > do the conversion for me?
    >
    > Angus
    >
    >
     
    Jim Langston, Jan 14, 2007
    #4
  5. Angus

    Gregg N Guest

    On Sat, 13 Jan 2007 22:16:45 +0000, Angus wrote:

    > But I have some ascii values as hex - eg 0xa1. I can't do this:
    >
    > oss << 0xa1;
    >
    > Well, I can but I just get the decimal number of the value added. I just
    > want to append the ascii value to the string - it is a funny i sort of
    > character in this example.


    By itself, 0xa1 is of type "int", so the compiler selects the "int"
    version of the insertion operator. If you want it to be a char, you
    must use a cast, store the value in a variable of type char, or use an
    escaped character literal. For example,

    oss << static_cast<char>(0xa1);

    or

    char var = 0xa1;
    oss << var;

    or

    oss << '\xa1';

    Gregg
     
    Gregg N, Jan 14, 2007
    #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. Victor Bazarov
    Replies:
    0
    Views:
    850
    Victor Bazarov
    Jun 25, 2003
  2. Russell Hanneken
    Replies:
    0
    Views:
    895
    Russell Hanneken
    Jun 25, 2003
  3. Alex Vinokur

    vector of ostringstream

    Alex Vinokur, Jul 10, 2003, in forum: C++
    Replies:
    4
    Views:
    699
    llewelly
    Jul 14, 2003
  4. Mark Warren
    Replies:
    3
    Views:
    16,560
    Sam Holden
    Jul 23, 2003
  5. Replies:
    10
    Views:
    958
Loading...

Share This Page