Conversions from decimal to binary,hex

Z

zgfareed

My program converts decimal numbers from to binary and hexadecimal. I
am having trouble with my output which is supposed to be in a certain
format. Binary needs to be in the format of
XXXX XXXX for numbers 0 to 255. What am doing wrong or not doing with
my output?

Source code:
// code

#include <iostream>
#include <iomanip>

using namespace std;

void binary(int);
void hexdecimal(int);

int main()
{
int array[256];
int hex[256];
int i;

cout << "DECIMAL " << " BINARY " << " HEXADECIMAL " <<
" BCD" << endl;
cout << endl;
for(int i=0;i<256;i++)
{
array = i;
cout << i;
binary(i);
hexdecimal(i);
cout<< endl;
}

return 0;
}


void binary(int i)
{
int rem;
if(i<=1)
{
cout << setw(8) << i;
return;
}
rem = i % 2;
binary(i >> 1);
cout << rem;
}

void hexdecimal(int i)
{
char buffer[33];
itoa(i,buffer,16);
cout << " ";
cout << setw(12) << buffer;

}

// end of code

This is what my output looks like for the first few

DECIMAL BINARY HEXADECIMAL

0 0 0
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
8 1000 8
9 1001 9
10 1010 a
11 1011 b

Thanks in advance
 
J

Jim Langston

My program converts decimal numbers from to binary and hexadecimal. I
am having trouble with my output which is supposed to be in a certain
format. Binary needs to be in the format of
XXXX XXXX for numbers 0 to 255. What am doing wrong or not doing with
my output?

Source code:
// code

#include <iostream>
#include <iomanip>

using namespace std;

void binary(int);
void hexdecimal(int);

int main()
{
int array[256];
int hex[256];
int i;

cout << "DECIMAL " << " BINARY " << " HEXADECIMAL " <<
" BCD" << endl;
cout << endl;
for(int i=0;i<256;i++)
{
array = i;
cout << i;
binary(i);
hexdecimal(i);
cout<< endl;
}

return 0;
}


void binary(int i)
{
int rem;
if(i<=1)
{
cout << setw(8) << i;
return;
}
rem = i % 2;
binary(i >> 1);
cout << rem;
}

void hexdecimal(int i)
{
char buffer[33];
itoa(i,buffer,16);
cout << " ";
cout << setw(12) << buffer;

}

// end of code

This is what my output looks like for the first few

DECIMAL BINARY HEXADECIMAL

0 0 0
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
8 1000 8
9 1001 9
10 1010 a
11 1011 b

Thanks in advance


Look at setfill('0') in combination with setw(12)
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
My program converts decimal numbers from to binary and hexadecimal. I
am having trouble with my output which is supposed to be in a certain
format. Binary needs to be in the format of
XXXX XXXX for numbers 0 to 255. What am doing wrong or not doing with
my output?

Source code:
// code

#include <iostream>
#include <iomanip>

using namespace std;

void binary(int);
void hexdecimal(int);

int main()
{
int array[256];
int hex[256];
int i;

cout << "DECIMAL " << " BINARY " << " HEXADECIMAL " <<
" BCD" << endl;
cout << endl;
for(int i=0;i<256;i++)
{
array = i;
cout << i;
binary(i);
hexdecimal(i);
cout<< endl;
}

return 0;
}


void binary(int i)
{
int rem;
if(i<=1)
{
cout << setw(8) << i;
return;
}
rem = i % 2;
binary(i >> 1);
cout << rem;
}


Your binary function is bugged:
1. use setfill('0') to pad with 0
2. the end of reccursion write a width of 8 but you may have alreday
written ones and zeroes before

You'd rather use an iterative algotithm:
for(int b=7;b>=0;--b)
{
cout<<((i>>b)&1);
}
cout<<endl;
void hexdecimal(int i)
{
char buffer[33];
itoa(i,buffer,16);
cout << " ";
cout << setw(12) << buffer;
}

You can use the hex manipulator (don't forget to reset it to dec after
or in other functions - alternatively use resetioflags() at the
beginning of your functions.

You feel like it, you could also implement your own 'bin' manipulator.
See Overload 55 for hints on how to do it.
// end of code
[snip]
 
J

James Kanze

My program converts decimal numbers from to binary and hexadecimal. I
am having trouble with my output which is supposed to be in a certain
format. Binary needs to be in the format of
XXXX XXXX for numbers 0 to 255. What am doing wrong or not doing with
my output?

Lot's of things:).

I presume that this is more or less homework, or an exercise for
you to learn by. Otherwise, presumably, you would use the hex
manipulator to output the hex. (Regrettably, there is no binary
manipulator.)
Source code:
// code
#include <iostream>
#include <iomanip>
using namespace std;

Something to avoid in production code, of course.
void binary(int);
void hexdecimal(int);

Just wondering, do you want the values to really be signed?
(Not that it matters for your test values.)
int main()
{
int array[256];
int hex[256];
int i;

What are these declarations for, here? You never use hex or i
at all, and you only write to array, you never read it.
cout << "DECIMAL " << " BINARY " << " HEXADECIMAL " <<
" BCD" << endl;

That could be one single string, of course.
cout << endl;
for(int i=0;i<256;i++)
{
array = i;
cout << i;
binary(i);
hexdecimal(i);
cout<< endl;
}
return 0;
}

void binary(int i)
{
int rem;
if(i<=1)
{
cout << setw(8) << i;
return;
}
rem = i % 2;
binary(i >> 1);
cout << rem;
}

Several general comments:

-- A function should never output to std::cout. Pass it an
std::eek:stream& as a parameter. Or have it return an
std::string.

-- You also don't take into consideration the fact that i might
be negative. If this is a pre-condition, then you should
have at least an assert; otherwise, you need to handle it.

-- Recursion is not particularly the best solution here;
although it can definitely be made to work, it handles
formatting very awkwardly. (It is a reasonable solution if
you generate non-formatted output, and then use a separate
routine to copy-format later. More on that below.)

-- You don't need the variable rem, either. Just output i % 2.

-- You definitely don't want the setw in the output in the if.
Each iteration outputs exactly one character. If the rest
worked (i.e. the condition in the if were correct), then
you'd have already output 7 digits before entering the if.

-- You're end condition doesn't take into account the number of
digits generated, at all.

-- You've no logic what so ever to generate the space in the
middle of the output.

If you're going to use recursion, and also want to format when
doing so, then you'll need information concerning the number of
digits and the format to be passed in each recursion. A
recursive solution which does both rapidly becomes painful.
void hexdecimal(int i)
{
char buffer[33];
itoa(i,buffer,16);
cout << " ";
cout << setw(12) << buffer;
}

The first two comments for binary also apply here. In addition,
there is no function itoa in C++.

Note that converting an int (or an unsigned int) to any base is
roughly the same. Without formatting, you can use either
recursion or iteration:

Iteration:

std::string
toString( unsigned int i, int base )
{
assert( base >= 2 && base <= 36 ) ;
std::string result ;
while ( i != 0 ) {
result += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[ i %
base ] ;
i /= base ;
}
std::reverse( result.begin(), result.end() ) ;
return result ;
}

Recursion:

std::string
toString( unsigned int i, int base )
{
std::string result ;
if ( i >= base ) {
result = toString( i / base, base ) ;
}
result += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[ i % base ] ;
return result ;
}

(Note that I've used unsigned int as an argument, to avoid the
issue of the sign.)

To ensure proper formatting, there are two solutions: do it
during the conversion, or use a copy-format after conversion.
To do it during converion, you have to pass the formatting
information to the "toString" function, above. Something like:

struct Spacing
{
int grouping ;
char sepChar ;
} ;

std::string
toString( unsigned int i, int base, Spacing const& fmt )
{
assert( base >= 2 && base <= 36 ) ;
std::string result ;
int generated = 0 ;
while ( i != 0 ) {
result += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[ i %
base ] ;
++ generated ;
if ( generated % fmt.grouping == 0 ) {
result += fmt.sepChar ;
}
i /= base ;
}
std::reverse( result.begin(), result.end() ) ;
return result ;
}

You can work out something similar for the recursive version if
you want, but note that the above uses an additional state
variable, which must be passed during recursion.

The alternative solution would be to use some sort of
copy-format function; a function which takes a format string
(e.g. something like the PICTURE clause in Cobol, or the format
string in a PRINT USING in Basic), and copies the unformatted
converted text using that, e.g.:

std::string
copyFormat( std::string const& format, std::string const& number )
{
std::string result ;
typedef std::string::const_reverse_iterator
SrcIter ;
SrcIter src = number.rbegin() ;
for ( SrcIter fmt = format.rbegin() ;
fmt != format.rend() ;
++ fmt ) {
switch ( *fmt ) {
case '#' :
result.push_back( src == number.rend()
? ' '
: *src ++ ) ;
break ;

case '9' :
result.push_back( src == number.rend()
? '0'
: *src ++ ) ;
break ;

default:
result.push_back( *fmt ) ;
break ;
}
}
std::reverse( result.begin(), result.end() ) ;
return result ;
}

In this case, you're output in the loop in main would be
something like:

std::cout << std::setw( 12 ) << i
<< copyFormat( " 9999 9999", toString( i, 2 ) )
<< copyFormat( " ####", toString( i, 16 ) )
<< std::endl ;

(Note that all of the above code is just off the top of my head;
none of it has been tested, or even compiled. So there are
probably a few errors. But it should give you some ideas to
start with.)
 
B

BobR

My program converts decimal numbers from to binary and hexadecimal. I
am having trouble with my output which is supposed to be in a certain
format. Binary needs to be in the format of
XXXX XXXX for numbers 0 to 255. What am doing wrong or not doing with
my output?

// ------------
void ToBinary( unsigned char const val, std::eek:stream &out){
for( int i(7); i >= 0; --i ){
if( val & (1 << i) ) out << "1";
else out << "0";
if( 4 == i ) out << " ";
} // for(i)
} // ToBinary(unsigned char const,ostream&)
// ------------
int main(){
int array[256];
cout<<"DECIMAL BINARY HEXADECIMAL BCD\n"<<endl;
for( size_t i(0); i < 256; ++i ){
array = i;
cout << i;


// > binary(i);

unsigned char numc( i & 0xFF );
ToBinary( numc, std::cout );
std::cout<<"\t";
hexdecimal(i);
cout<< endl;
}
return 0;
}

Look up 'std::bitset' for another way to do it.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top