this is my code to convert characters into its decimal form...it is working and the next step is to convert the decimal form to its binary form...how can i do that....????

#include<iostream>
#include<conio.h>
using namespace std;
void input();
void process();

int main()
{
 input();
 system("PAUSE");
}

void input()
{
string msg; 
cout<<"Enter message: ";
getline(cin, msg);
cout<<endl;
// convert character to binary
cout<<"CHAR"<<"\t"<<"\t"<<"DECIMAL"<<endl;
cout<<"----"<<"\t"<<"\t"<<"-------"<<endl;

for(int count=0;count<msg.size();count++)
{
        
        cout<<msg[count]<<"\t"<<"="<<"\t"<<static_cast<int>(msg[count])<<endl;
}

Recommended Answers

All 13 Replies

You should use
/**************************/
#include <conio.h>
. . .
while(!kbhit());
getch();
. . .
/*****************************/
to view the pressed key.

covert decimal to binary:

why don't use itoa(buf, number, 2);????

It' works like this:
int remainder = number%2
strcat(buf, remainder);
if aliquot > 0 continue doing this.

I hope you'll see this information helpful...
Write PM for more

commented: you posted nonsense -4

>>I hope you'll see this information helpful...
Oh God how much more could you be wrong! To OP: forget the crap that he just posted, its nonsense.

It's not a crap and it's works!

>It's not a crap
Really? Let's take a look:

You should use
/**************************/
#include <conio.h>
. . .
while(!kbhit());
getch();
. . .
/*****************************/
to view the pressed key.

You realize that this totally defeats the purpose of not using system("pause") for portability reasons, right? conio.h isn't a standard header, so if you use it, you're basically locking yourself into a single compiler. Your suggestion in this case is crap.

covert decimal to binary:

why don't use itoa(buf, number, 2);????

Because itoa isn't a standard function and the OP's compiler might not support it? Because this is clearly a homework assignment and "cheating" with a simple non-manual solution isn't going to be acceptable? Your suggestion in this case is crap.

It' works like this:
int remainder = number%2
strcat(buf, remainder);
if aliquot > 0 continue doing this.

Not only is this completely nonsensical, it's broken in every conceivable way. This suggestion is very crap.

>and it's works!
Using kbhit to wait for a key probably works on your system and on your compiler. Beyond that, you're SOL because the conio library isn't standard and its behavior is implementation-dependent. Using itoa to convert a value to binary might work on your system and your compiler. Beyond that you're SOL because itoa isn't standard and its behavior is implementation-dependent. The rest of your code is guaranteed not to work.

Oh conio.h isn't a standart header, so you use

int key;
cin>>key;

????????????????

iostream.h might be better

Also conio.h is better then

system("PAUSE");

In the decimal to binary algorithm I wanted to describe another points.

To convert you need to devide a number for module and take the 1 or 0.
You do this before number is more than 2. I'll check some words cause of my bad English ))

>????????????????
That's a reasonable implementation, but it requires a spurious variable. You could just say cin.get(); and call it good.

>iostream.h might be better
iostream.h isn't standard either (it never was). It's iostream without the .h extension.

>Also conio.h is better then
>system("PAUSE");
How so?

>In the decimal to binary algorithm I wanted to describe another points.
Let me put it this way: I understand that algorithm very well (probably better than you), and I can't fathom how anyone could figure out how it works and how to implement it given that description.

So if you know standart headers can you give me a list?

I'll review the algorithm...

Your idea of cin.get() is good )))

commented: for accepting advice. +7

>So if you know standart headers can you give me a list?

C++:

<algorithm> <array> <bitset> <cassert> <cctype> <cerrno> <cfloat> <ciso646> <climits> <clocale> <cmath> <complex> <csetjmp> <csignal> <cstdarg> <cstddef> <cstdio> <cstdlib> <cstring> <ctime> <cwchar> <cwctype> <deque> <exception> <fstream> <functional> <iomanip> <ios> <iosfwd> <iostream> <istream> <iterator> <limits> <list> <locale> <map> <memory> <new> <numeric> <ostream> <queue> <set> <sstream> <stack> <stdexcept> <streambuf> <string> <strstream> <typeinfo> <utility> <valarray> <vector>

C89:

<assert.h> <ctype.h> <errno.h> <float.h> <iso646.h> <limits.h> <locale.h> <math.h> <setjmp.h> <signal.h> <stdarg.h> <stddef.h> <stdio.h> <stdlib.h> <string.h> <time.h> <wchar.h> <wctype.h>

What is C89? I see this first!

C89 is the previous C standard, but because the new standard (C99) isn't widely implemented, we basically pretend that the C programming language is a common subset of the two.

Instead of writing code for you, I would introduce you to the Number System. Nowaday, there are 4 type of numbers that are commonly use: Decimal (base 10), Binary (base 2), Octal (base 8) and Hexadecimal (base 16). Decimal is what we are using everyday, and Binary, Octal and Hexadecimal are used in computer.

Why Decimal is a base 10 number? To explain this, I would give you an example of a decimal number, because the example is self-explained. (I am using sign ^ to represent the power. for example: 10^2 mean 10 power 2)

4563 = 4x(10^3) + 5x(10^2) + 6x(10^1) + 3x(10^0)

Similarly to base 10(decimal), the other base also follow the same pattern.
Binary: 11011 = 1x(2^4) + 1x(2^3) + 0x(2^2) + 1x(2^1) + 1x(2^0)
Octal: 563 = 5x(8^2) + 6x(8^1) + 3x(8^0)
Hexadecimal: F6 = 15(16^1) + 6x(16^0)

To convert a decimal number into other base number, we simply divide the decimal number with the base until it reach to zero, and those reminds will become a number in other base number. For example: 47 decimal number convert to Octal number(base 8):

47 / 8 = 5 reminder 7
5 / 8 = 0 reminder 5

so 47 in decimal number equal to 57 in Octal number. Now, I will convert 47 decimal number to Binary number(base 2).

47 / 2 = 23 reminder 1
23 / 2 = 11 reminder 1
11 / 2 = 5 reminder 1
5 / 2 = 2 reminder 1
2 / 2 = 1 reminder 0
1 / 2 = 0 reminder 1

so 47 in decimal number equal to 101111 in binary number.

I hope this is helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.