Solved

stream input/output:  output decimal to binary on the screen

Posted on 2004-03-23
4
241 Views
Last Modified: 2011-09-20
Hi,

I have a simple assignment where I have to convert 5 digits from an int array 'a'  to binary, oct,  dec, and  hex.
The oct, dec, hex are no problem, but I cannot figure out how to convert the decimal values to binary.  I know there is a simple way to do this without writing an algorithm, but I'm at a loss.  Besides, I'm not SUPPOSED to write an algorithm.
tried ios::binary too, but no luck.

code snippet of my output from the array is below followed by how its supposes to look
BTW: width8 is self-defined stream manipulator I wrote to format output to 8 spaces wide:

 for ( int i = 0; i < size; i++ ){
       cout << width8 <<  -->>binary convertion here? <--  << a[i];  //binary
                                                                                                
       cout << width8 << showbase << oct << a[i] << noshowbase;  //oct
       cout << width8 << dec << a[i];                                              //dec
       cout << width8 << uppercase << hex << a[i];                        //hex
       cout << endl;
       }
 cout << endl;



Table with the numbers in various bases
bin        oct      dec     hex
1010      012      10      A
10100    024      20      14
11110    036      30      1E
101000  050      40      28
110010  062      50      32
 
0
Comment
Question by:r3r
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 500 total points
ID: 10664225
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10664232
0
 

Author Comment

by:r3r
ID: 10664694
thanks all.  i appreciate it.
R
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 10666072
The standard library also has bitset, which is nice for binary numbers, if you are OK with leading zeros:
--------8<--------
#include <iostream>
#include <iomanip>
#include <bitset>
#include <vector>

int main()
{
        const int size = 5;
        std::vector<int> a(size);
        for (int i = 0;i < size;++i)
                a[i] = 10*(i+1);
        for (int i = 0;i < size;++i)
                std::cout
                        << std::setw(8) << std::bitset<6>(a[i]) /* Six bit bitset */
                        << std::setw(8) << std::oct << std::showbase << a[i]
                        << std::setw(8) << std::hex << std::showbase << a[i]
                        << std::setw(8) << std::dec << std::showbase << a[i]
                        << '\n';
}
--------8<--------
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question