Stream Output: operator << ()

unsigned_int<> provides a templated overload of:

std::basic_ostream<..> &operator << (
    std::basic_ostream<..> &os, unsigned_int<...> const & uint
);

This operator writes is right hand side to the output stream.

Tests

Decimal Output

Vanilla

stringstream oss;
char const r_init[] = "358024680358024680358024679";

unsigned_int< 256 >  r( r_init );

oss << r;

cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == r_init);

unsigned_int< 256 > res;
oss >> res;
CHECK( oss );
CHECK( res == r );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

[358024680358024680358024679]
Ok

Padded

ostringstream oss;
char const r_init[] = "358024680358024680358024679";

unsigned_int< 256 >  r( r_init );

oss << setw( 60 ) << r;
string s = string( 61 - countof( r_init ), ' ' ) + r_init;

cout << "<" << s << ">\n";
cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == s );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

<                                 358024680358024680358024679>
[                                 358024680358024680358024679]
Ok

Pad and Fill

ostringstream oss;
char const r_init[] = "358024680358024680358024679";

unsigned_int< 256 >  r( r_init );

oss << setw( 60 ) << setfill( '-' ) << r;
string s = string( 61 - countof( r_init ), '-' ) + r_init;

cout << "<" << s << ">\n";
cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == s );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

<---------------------------------358024680358024680358024679>
[---------------------------------358024680358024680358024679]
Ok

Fill Left

ostringstream oss;
char const r_init[] = "358024680358024680358024679";

unsigned_int< 256 >  r( r_init );

oss << setw( 60 ) << setfill( '-' ) << left << r;
string s = r_init + string( 61 - countof( r_init ), '-' );

cout << "<" << s << ">\n";
cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == s );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

<358024680358024680358024679--------------------------------->
[358024680358024680358024679---------------------------------]
Ok

std::showpos (+)(Do Nothing)

ostringstream oss;
char const r_init[] = "358024680358024680358024679";

unsigned_int< 256 >  r( r_init );

oss << setw( 60 ) << showpos << r;
string s = string( 61 - countof( r_init ), ' ' ) + r_init;

cout << "<" << s << ">\n";
cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == s ); /* i.e. no '+' */

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

<                                 358024680358024680358024679>
[                                 358024680358024680358024679]
Ok

Fat (wider than std::setw())

stringstream oss;

char const init[] = "358024680358024680358024679";

unsigned_int< 256 >  r( init );

oss << -r;

cout << "[" << oss.str() << "]\n";

unsigned_int< 256 > res;
oss >> res;
CHECK( oss );
CHECK( res == -r );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

[115792089237316195423570985008687907853269984665640206014777225983232771615257]
Ok

Hexadecimal Output

Vanilla

ostringstream oss;
char const lwr[] = "0x123456789abcdef0123456789abcdef";

unsigned_int< 256 > x( lwr );

oss << hex << x;

cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == (lwr + 2) );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

[123456789abcdef0123456789abcdef]
Ok

Upper

ostringstream oss;
char const lwr[] = "0x123456789abcdef0123456789abcdef";
char const upr[] = "0X123456789ABCDEF0123456789ABCDEF";

unsigned_int< 256 > x( lwr );

oss << hex << uppercase << x;

cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == ( upr + 2 ) );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

[123456789ABCDEF0123456789ABCDEF]
Ok

Show Base

ostringstream oss;
char const lwr[] = "0x123456789abcdef0123456789abcdef";

unsigned_int< 256 > x( lwr );

oss << hex << showbase << x;

cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == lwr );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

[0x123456789abcdef0123456789abcdef]
Ok

Show Base Upper

ostringstream oss;
char const lwr[] = "0x123456789abcdef0123456789abcdef";
char const upr[] = "0X123456789ABCDEF0123456789ABCDEF";

unsigned_int< 256 > x( lwr );

oss << hex << showbase << uppercase << x;

cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == upr );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

[0X123456789ABCDEF0123456789ABCDEF]
Ok

Set Width (setw) and Show base

ostringstream oss;
char const lwr[] = "0x123456789abcdef0123456789abcdef";
char const upr[] = "0X123456789ABCDEF0123456789ABCDEF";

unsigned_int< 256 > x( lwr );

oss << hex << showbase << uppercase << setw( 60 ) << x;

string s = string( 61 - countof( upr ), ' ' ) + upr;

cout << "<" << s << ">\n";
cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == s );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

<                           0X123456789ABCDEF0123456789ABCDEF>
[                           0X123456789ABCDEF0123456789ABCDEF]
Ok

Octal Output

Vanilla

ostringstream oss;
char const lwr[] = "7x1234567012345670123456701234567";

unsigned_int< 256 > x( lwr );

oss << oct << x;

cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == (lwr + 2) );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

[1234567012345670123456701234567]
Ok

Show Base

ostringstream oss;
char const lwr[] = "7x1234567012345670123456701234567";
char const zlwr[] = "01234567012345670123456701234567";

unsigned_int< 256 > x( lwr );

oss << oct << showbase << x;

cout << "[" << oss.str() << "]\n";

CHECK( oss.str() == zlwr );

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

[01234567012345670123456701234567]
Ok