i would like to create a program that converts a number system to another

i found few ideas on how to do this but lacks explanation so i can't figure out 'how it worked'.
found that 'for loop' over the net,did some editing then applied it in a program.

here's a code

``````#include <iostream.h>
#include <math.h>

int main()
{
int bin,n,r,x;
char a;//looping

do
{
int s=0;
system("cls");
cout<<"Number System Conversion Program"<<endl;
cout<<"\nEnter binary: ";
cin>>bin;

n=bin;
for(x=0;n!=0;x++)
{
r=n%10;//this is the part
s=s+r*(int)pow(2,x);//im having a hard time
n=n/10;//to figure out
}
cout<<"binary: "<<bin<<"\ndecimal: "<<s;
cout<<"\nenter new number?[y/n]: ";
cin>>a;
}while((a=='y')||(a=='Y'));
system("cls");
}``````

1)can someone be kind enough to explain that part in the 'for loop'?

2)whenever i enter 1000000000000000,the program stops responding.
i tried to declare everything as unsigned int or even unsigned long but still same problem.

Since an int defaults to decimal on input, and the largest value an int can hold is less then 5,000,000,000 it's no wonder entering more than that doesn't work.

You can change your code to enter a string instead an int and process the number character by character.

I thınk you can't gıve bınary number like an integer.The number which u are giving is taken as an integer..i have used code for this conversion with predefined libraries...

``````#include <iostream>
#include <bitset>
#include <string>

using namespace std;

int main()
{
string s;

cout<<"Enter the bınary number u want to convert ın to the decımal"<<endl;
cin>>s;

cout << "\"1000101011\" as number:  "
<< bitset<100>(string(s)).to_ulong() << endl;

return 0;
}``````

awesome. didn't know there's a library for that
(well i just started learning different programming languages so what do you expect lol)

in the <bitset> library,can i convert any number system to another?
i tried googling but i don't even get the process on how that binary input is converted into decimal.

is there a way i can fix this long line:

``cout<<"\n               ##                                 \n  ## # # ###  #   ## ### ###      ## ### ###  ## \n  #  # # #   ### # # #   ##      # # #   ##  # # \n ##  ### #    #  ### ### ###     ### #   ### ### \n             ##                                  \n\n              #           #       #          \n     ###  ##  #  ### # #  #   ## ### ### ### \n     #   # #  #  #   # #  #  # #  #  # # #   \n    ### ###  ## ### ###  ## ###  ## ### #"<<endl<<endl;``

is there an escape sequence that lets me divide this long single line into multiple lines
BUT only affects the source code?

``````cout << "Line1 in source"
<< "Line2 in source"
<< endl; // new line in output``````
``````cout << "Line1 in source"
<< "Line2 in source"
<< endl; // new line in output``````

lol

> is there an escape sequence that lets me divide this long single line into multiple lines
no escape sequence is required. a literal string can span multiple line (with quotes).

``````const char long_cstr[] = "this is the first line\n"
"and this is the second line\n"
"and you could have more lines\n" ;``````

>in the <bitset> library,can i convert any number system to another?
Not directly, but you can use predefined stream manipulators and bitset to get conversions between binary, octal, decimal, and hexadecimal. However, if this is for homework, this solution probably won't be acceptable.

>1)can someone be kind enough to explain that part in the 'for loop'?
Sure, for the scrolling impaired, here's the code in question:

``````for ( x = 0; n != 0; x++ )
{
r = n % 10;
s = s + r * (int)pow(2, x);
n = n / 10;
}``````

The poor variable names are a problem, but oh well. The first thing to understand is that by saying `value % base` will give you the least significant digit from the value in that base. So 12345 % 10 gives you 5, 532 % 10 gives you 2. Likewise, `value / base` removes the least significant digit. When you've removed all of the least significant digits, it reduces the value to 0. You can use those two operations and that rule together to process each digit of a number:

``````#include <iostream>

int main()
{
int value;

std::cout<<"Enter a number: ";

if ( std::cin>> value ) {
while ( value != 0 ) {
std::cout<<"Digit: "<< value % 10 <<'\n';
value = value / 10;
}
}
}``````

The nice thing about this is that an integer in memory doesn't have a base representation. The base only depends on how you choose to represent the value when you process it. I could just as easily use the exact same code, except using base 8 to get the octal digits of a decimal value (decimal is the default representation for input):

``````#include <iostream>

int main()
{
int value;

std::cout<<"Enter a number: ";

if ( std::cin>> value ) {
while ( value != 0 ) {
std::cout<<"Digit: "<< value % 8 <<'\n';
value = value / 8;
}
}
}``````

Here's where it gets a bit confusing. Your inputting a decimal value, but that decimal value happens to represent a binary value. That's why the loop is extracting decimal digits, and it's also why the processing part has to take a digit and pretend that it's a binary digit:

``s = s + r * (int)pow(2, x);``

Binary digits are powers of two. The pow function is giving you each successive power of two because x is counting up from 0. If r represents an unset bit, the value will be 0 and nothing happens because multiplying by zero gives zero and adding 0 to a value gives the same value.

If r is 1, that power of 2 is added to the total. Given any binary value, you can take the power of 2 for each set bit, add them together, and get the decimal representation of that value. Let's use 01000011 as an example (the decimal representation is 67. The set bits are at positions 0, 1, and 6. Here are those powers of 2:

2^0 = 1
2^1 = 2
2^6 = 64

1 + 2 + 64 = 67, which is the decimal representation of 01000011. We just did manually what the program does: find each set bit and add the power of 2 for that bit to a running total. The running total is the decimal representation of the binary value.

>2)whenever i enter 1000000000000000,the program stops responding.
Remember that the input is still a decimal value, so you're restricted to the limits of the integer type. If your integer is 32-bits, that means you can only use 10 digits for input rather than 32 because the largest value a 32-bit integer can hold is 10 digits (4294967296).

To get the full range of integer values in binary, you have to use strings.

The poor variable names are a problem

lol that's what i thought.
atleast putting some comments would've helped.

anyway,
awesome explanation..

here's what i've got:

``````#include <iostream.h>
#include <math.h>
main()
{
int value,temp1,output=0,temp;
int x;//counter of place value
std::cout<<"Enter a number: ";
std::cin>> value;

temp1 = value;
for ( x=0;temp1!=0;x++)
{
temp = temp1 % 8;
output = output + temp * (int)pow(10,x);
temp1 = temp1 / 8;
}
cout<<"\ndecimal input value in octal system is "<<output<<endl;
}``````

tell me if i'm getting this right:
i'll change the power to what the base of input is.
temp1 % z and temp1 / z where z = base of output.

so with the code above,with some changes...
i'll be able to do ALL of these tasks:
bin to dec,bin to oct,oct to bin,oct to dec,dec to bin,dec to oct.

if yes...awesome,i'm learning.lol
most probably i'll finish my program with the tasks above by tomorrow
i want to finish it now already but my eyes desperately need some rest XD

so far,things left that i have no clue how:
1)any numsystem to hex so as vice versa
2)AND mostly, to convert inputs with fractionals(this one's i really got to know)

You also able to optimize your code by doing this:

``````for ( [B]x=1[/B] ; temp1 > 0 ; [B]x*=10[/B])
{
temp = temp1 % 8;
output = output + temp * [B]x[/B];
temp1 = temp1 / 8;
}``````

It would preform faster than before.

One problem you're going to run into is cin can only input decimal values. You cannot enter binary, octal, and hex natively. You'd need to enter them as a string and convert the characters into the numeric value. Then you'll be able to convert them to another base.

here's my logic:

yes the input is basically as string.
but what i'm thinking is that there is a way where for example
he program requires the user to input only OCTAL values,
if the user input strings that aren't found in the octal number system
program asks the user to input again.

it's like pretending that the input is not a string but is a number in octal base.

how am i going to do that? i'm not yet sure exactly how
maybe by using <string.h>?maybe there's a function in this library that when this certain character is found in the string,program then takes the input as invalid and asks the user again to input a new one.

if my logic makes no sense,care to enlighten me? ;)

Rather than "the input is basically as string" just input a string. I don't know how you'll input "basically a string". :icon_lol:

Yes, you're logic is right on. Input the string. Then check each character to make sure they don't fall outside the base. Then convert the characters one by one into one integer and you have your octal/hex/binary value. Now you can convert it into any other base.

``````input string [I]str[/I]
set [I]number[/I] to 0
Loop starting at [I]str[0][/I]
check if the character is within the base (if not, start over)
convert the character to a [I]value[/I] ('0'=>0, '2'=>2)
End loop when you reach last character or \n.``````

Rather than "the input is basically ASstring" just input a string. I don't know how you'll input "basically a string"

lol

anyway,i'll try to finish the "oct/bin/dec" later :D

by the way, thanks to everyone who replied here to help especially to Narue and his gnarly explanation with the for loop :D

>One problem you're going to run into is cin can only input decimal values.
You can use the hex and oct manipulators with cin to read hexadecimal and octal values as well:

``````#include <iostream>

int main()
{
int value;

std::cout<<"Enter a hex value: ";
std::cin>> std::hex >> value;
std::cout<< std::hex << value
<<" in decimal is "
<< std::dec << value <<'\n';
}``````

Binary is the only one you need a string to work with properly.

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.