Why don't string support binary?

I

Immortal Nephi

I am curious. Why don't iostream libary support binary conversion?
Let's say for example. You want to use "cout << hex << xx << endl"
What about "cout << binary << xx << endl"? No such binary exist.
Take a look at my code below. First example is the best option, but I
prefer second example. Do you have other ways that binary will be
supported?


string convBase(unsigned long v, long base)
{
string digits = "0123456789abcdef";
string result;
if((base < 2) || (base > 16)) {
result = "Error: base out of range.";
}
else {
do {
result = digits[v % base] + result;
v /= base;
}
while(v);
}
return result;
}

int main()
{
// First Example
unsigned long x = 0xABCD;
cout << "???: " << convBase(x,4) << endl;
cout << "Hex: " << convBase(x,16) << endl;
cout << "Decimal: " << convBase(x,10) << endl;
cout << "Octal: " << convBase(x,8) << endl;
cout << "Binary: " << convBase(x,2) << endl;
cout << "Test: " << convBase(x,32) << endl;

// Second Example
int i = 0x0203;
std::string s;
std::eek:stringstream out;

out << hex << i;
s = out.str();

cout << "-->" << s << "\n" << out.str() << endl;

return 0;
}
 
J

joshuamaurice

        I am curious.  Why don't iostream libary support binary conversion?
Let's say for example.  You want to use "cout << hex << xx << endl"
What about "cout << binary << xx << endl"?  No such binary exist.
Take a look at my code below.  First example is the best option, but I
prefer second example.  Do you have other ways that binary will be
supported?

string convBase(unsigned long v, long base)
{
        string digits = "0123456789abcdef";
        string result;
        if((base < 2) || (base > 16)) {
                result = "Error: base out of range.";
        }
        else {
                do {
                        result = digits[v % base] + result;
                        v /= base;
                }
                while(v);
        }
        return result;

}

int main()
{
// First Example
        unsigned long x = 0xABCD;
        cout << "???:     " << convBase(x,4) << endl;
        cout << "Hex:     " << convBase(x,16) << endl;
        cout << "Decimal: " << convBase(x,10) << endl;
        cout << "Octal:   " << convBase(x,8) << endl;
        cout << "Binary:  " << convBase(x,2) << endl;
        cout << "Test:    " << convBase(x,32) << endl;

// Second Example
        int i = 0x0203;
        std::string s;
        std::eek:stringstream out;

        out << hex << i;
        s = out.str();

        cout << "-->" << s << "\n" << out.str() << endl;

        return 0;
}

You should be careful. Binary generally means something else in this
context, something more like
int x;
cout.write(reinterpret_cast<char*>(&x), sizeof(x));
which is not portable.

You want to convert a int to a sequence of 0 and 1, that kind of
binary representation. Why is it not in the standard? I guess it
wasn't very popular.
 
J

joshuamaurice

You want to convert a int to a sequence of 0 and 1, that kind of
binary representation. Why is it not in the standard? I guess it
wasn't very popular.

Err, '0' and '1', a sequence of char.
 
J

James Kanze

I am curious. Why don't iostream libary support binary conversion?

Historical reasons, I suppose. In C, printf and scanf didn't
support binary, so nobody bothered defining it for ostream and
istream either. In both cases, an extension to support it would
be legal; the fact that no implementor has bothered suggests
that there isn't much demand for it. Or rather that those that
want or need it don't have very much commercial clout.
Let's say for example. You want to use "cout << hex << xx << endl"
What about "cout << binary << xx << endl"? No such binary exist.
Take a look at my code below. First example is the best option, but I
prefer second example. Do you have other ways that binary will be
supported?
string convBase(unsigned long v, long base)
{
string digits = "0123456789abcdef";
string result;
if((base < 2) || (base > 16)) {
result = "Error: base out of range.";
}
else {
do {
result = digits[v % base] + result;
v /= base;
}
while(v);
}
return result;
}
int main()
{
// First Example
unsigned long x = 0xABCD;
cout << "???: " << convBase(x,4) << endl;
cout << "Hex: " << convBase(x,16) << endl;
cout << "Decimal: " << convBase(x,10) << endl;
cout << "Octal: " << convBase(x,8) << endl;
cout << "Binary: " << convBase(x,2) << endl;
cout << "Test: " << convBase(x,32) << endl;
// Second Example
int i = 0x0203;
std::string s;
std::eek:stringstream out;
out << hex << i;
s = out.str();
cout << "-->" << s << "\n" << out.str() << endl;
return 0;
}

I don't quite see how the second example supports binary.

I'd probably do something like:

template< typename UInt > // constraint: UInt is
class BaseN // unsigned integral type
{
public:
explicit BaseN( UInt value, int base )
: myValue( value )
, myBase( base )
{
assert( base > 1 && base <= 36 ) ;
}
void print( std::eek:stream& dest ) const ;
friend std::eek:stream&operator<<( std::eek:stream& dest,
BaseN const& value )
{
value.print( dest ) ;
return dest ;
}

private:
UInt myValue ;
unsigned myBase ;
} ;

template< typename UInt >
void
BaseN< UInt >::print(
std::eek:stream& dest ) const
{
char buffer[ std::numeric_limits< UInt
::digits + 1 ] ;
char* p = buffer + sizeof( buffer ) ;
UInt tmp = myValue ;
char const* digits
= ( (dest.flags() & std::ios::uppercase) != 0
? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
: "0123456789abcdefghijklmnopqrstuvwxyz" ) ;
*--p = '\0' ;
do {
*--p = digits[ tmp % myBase ] ;
tmp /= myBase ;
} while ( tmp != 0 ) ;
dest << p ;
}

template< typename UInt >
BaseN< UInt >
baseN( UInt value, int base )
{
return BaseN< UInt >( value, base ) ;
}

This would allow things like:
std::cout << baseN( someValue, 2 )
for a somevalue of any unsigned integral type, with any base
between 2 and 36.

Alternatively, it should be possible to define a manipulator
which sets a base and returns a wrapper to the stream which
intercepted integral conversions to convert using that base, so
that you could write:
std::cout << base( 2 ) << someValue ;
This is a bit tricker, however, and I've never found it worth
the extra effort.
 

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,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top