By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,440 Members | 1,411 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,440 IT Pros & Developers. It's quick & easy.

how to get a formatted output right?

P: n/a
Hi,
i want to get the adress of a pointer in a string. I did so by
outputting it to a ostringstream:
------------------------------
char* ptr;
//generate adress in hex format
std::ostringstream osstr,ossbyteSize ;
//This does not work anyway
//osstr.flags((std::ios_base::fmtflags)513);
//cout <<osstr.flags()<<endl;

osstr << &ptr;
std::string value = osstr.str();
cout << value<<endl;
---------------------------------
This works actually.. just the formatting is different when compiling
under Visual Studio and g++ Linux.
MSVC: 0022FFCC
Linux: 0x22ffcc
Id like to have a 8byte string containing the adress.. even if its lead
by some zeros.

Ok i said.. lets browse the net and set the formatting flags for both.
i got the flags of the windows version (513) and tried to set it for
both..(linux showed 4098).
Now linux also shows 513.. but the output is the same.

any ideas?
is this a maybe a compiler issue? could anybody reassure me that i did
the language part right?

Dec 19 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
mw****@freenet.de wrote:
Hi,
i want to get the adress of a pointer in a string. I did so by
outputting it to a ostringstream:
------------------------------
char* ptr;
//generate adress in hex format
std::ostringstream osstr,ossbyteSize ;
//This does not work anyway
//osstr.flags((std::ios_base::fmtflags)513);
Where did you get that magic value of 513?
//cout <<osstr.flags()<<endl;

osstr << &ptr;
std::string value = osstr.str();
cout << value<<endl;
---------------------------------
This works actually.. just the formatting is different when compiling
under Visual Studio and g++ Linux.
MSVC: 0022FFCC
Linux: 0x22ffcc
Id like to have a 8byte string containing the adress.. even if its lead
by some zeros.
Well, the address is printed in a compiler-specific format for pointers. You
can do what you want by converting the pointer into a suitably sized
integer before putting it into the stream.
Ok i said.. lets browse the net and set the formatting flags for both.
i got the flags of the windows version (513) and tried to set it for
both..(linux showed 4098).
Now linux also shows 513.. but the output is the same.
The flags have names. Use them. The values are compiler-specific
any ideas?
is this a maybe a compiler issue? could anybody reassure me that i did
the language part right?
Dec 19 '06 #2

P: n/a
Geo

mw****@freenet.de wrote:
Hi,
i want to get the adress of a pointer in a string. I did so by
outputting it to a ostringstream:
------------------------------
char* ptr;
//generate adress in hex format
std::ostringstream osstr,ossbyteSize ;
//This does not work anyway
//osstr.flags((std::ios_base::fmtflags)513);
//cout <<osstr.flags()<<endl;

osstr << &ptr;
std::string value = osstr.str();
cout << value<<endl;
---------------------------------
This works actually.. just the formatting is different when compiling
under Visual Studio and g++ Linux.
MSVC: 0022FFCC
Linux: 0x22ffcc
Id like to have a 8byte string containing the adress.. even if its lead
by some zeros.

Ok i said.. lets browse the net and set the formatting flags for both.
i got the flags of the windows version (513) and tried to set it for
both..(linux showed 4098).
Now linux also shows 513.. but the output is the same.

any ideas?
is this a maybe a compiler issue? could anybody reassure me that i did
the language part right?
If you want zero padded 8 digit hex values use this

cout << setfill('0') << setw(8) << hex << [value];

but it doesn't mean it will work for a pointer

Dec 19 '06 #3

P: n/a

Rolf Magnus wrote:
Where did you get that magic value of 513?
by reading it from the windows version...

Anyway i now got it right by switching to sprintf().
The flags have names. Use them. The values are compiler-specific
Thanks, i didn't thought of that. i somehow assumed that values would
mean the same... but i now realize that alone different CPU
architectures might change this.

I actually first tried to solve the problem by using the examples shown
here:

http://www.cplusplus.com/ref/iostrea...base/setf.html
http://www.cplusplus.com/ref/iostrea...ase/flags.html

but the first one wouldn't even compile in Linux(copy-pasted )! and
the second just didn't make any difference.
Both worked without problems on MSVC though.
I was using the same source file for both compilers...

Thanks for your responses!!

Dec 19 '06 #4

P: n/a
On 19 Dec 2006 04:06:19 -0800 in comp.lang.c++, mw****@freenet.de wrote,
>Anyway i now got it right by switching to sprintf().
You got what you wanted by luck. sprintf() formatting of pointers is
implementation defined, just as iostream formatting. It's an open
question whether you had good luck or bad luck, since sooner or later
you will meet an implementation where sprintf() formatting differs and
you have to fix it again.

Dec 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.