How to write data to a ostringstream?

A

Angus

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
 
A

Alan Johnson

Angus said:
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 ;
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Angus said:
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'.
 
J

Jim Langston

Angus said:
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?
 
G

Gregg N

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top