Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to write data to a ostringstream?

Reply
Thread Tools

How to write data to a ostringstream?

 
 
Angus
Guest
Posts: n/a
 
      01-13-2007
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


 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      01-13-2007
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
 
Reply With Quote
 
 
 
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      01-13-2007
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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-14-2007

"Angus" <(E-Mail Removed)> wrote in message
news:eobloo$o5r$1$(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Gregg N
Guest
Posts: n/a
 
      01-14-2007
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
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to stream or write data into a tar.gz file as if the data werefrom files? bwv549 Ruby 12 10-06-2008 02:01 PM
How to use Response.write to write to a specific area on a aspx pa =?Utf-8?B?QWJlbCBDaGFu?= ASP .Net 6 05-03-2006 10:16 PM
how to encrypt a C data and write a bin file and read a bin at run time and decrypt C data sweety C Programming 9 02-07-2006 05:28 PM
Backup Error: The device reported an error on a request to write data to media. Error reported: bad data. Ismaiel Computer Support 0 10-26-2004 11:42 AM



Advertisments