anybody knows how to convert a decimal into a binary, decimal to hexadecimal, binary to hexadecimal and decimal, hexadecimal to decimal and binary?

can you give me the code? pls

Printable View

- 12-02-2001Unregistereddecimal to binary, decimal to hexadecimal and vice versa
anybody knows how to convert a decimal into a binary, decimal to hexadecimal, binary to hexadecimal and decimal, hexadecimal to decimal and binary?

can you give me the code? pls - 12-02-2001Stoned_CoderQuote:

anybody knows how to convert a decimal into a binary, decimal to hexadecimal, binary to hexadecimal and decimal, hexadecimal to decimal and binary?

Quote:

can you give me the code? pls

If you want more help than that then post your efforts. - 12-02-2001GaPe
If you want to convert decimal to binary:

You have a number, let's say 65.

65 / 2 = 32 * 2 + 1

32 / 2 = 16 * 2 + 0

16 / 2 = 8 * 2 + 0

8 / 2 = 4 * 2 + 0

4 / 2 = 2 * 2 + 0

2 / 2 = 1 * 2 + 0

1 / 2 = 0 * 2 + 1

0 / 2 = 0 * 2 + 0

The result is: 01000001

And you read the difference from below to above.

If you want to convert binary to decimal:

You have a number, let's say 01000001.

0 * 2^7 + 1 * 2^6 + zero's + 1 * 2^0 = 65

It's the same with hexadecimal and octal number system. - 12-02-2001CheeseMonkey
since the topic is up, i figured i should put my problem here. i'm working on a program that converts binary to decimal. however, my validinput(char *pbin) function isn't working properly. it's supposed to make the do while loop keep looping if the input has more than 1' and 0's.

and then the bintodec(char *pbin) function doesn't add a one when it needs to, i have no clue why this doesn't work

here's my code

Code:`#include <iostream.h>`

//check if input is a binary number

int validinput(char *pbin)

{

for(int x = 0; x < 9; x++)

{

if (pbin[x] != '0' || pbin[x] != '1')

return 0;

}

if (pbin[8] != '\0')

return 0;

return 1;

}

//takes the binary string and outputs a decimal number it creates

int bintodec(char *pbin)

{

int base2[] = {128, 64, 32, 16, 8, 4, 2, 1};

int num = 0;

for(int x = 0; x < 8; x++)

{

if (pbin[x] == '1')

num += base2[x];

}

return num;

}

//main part of the program

int main()

{

char binary[9];

int decimalnum;

do

{

cout<<"Enter an 8 bit binary number: ";

cin.getline(binary, '\n');

}

while(!validinput(binary));

decimalnum = bintodec(binary);

cout<<"The decimal number is: "<< decimalnum;

return 0;

}

- 12-04-2001Unregistered
thanks gape, ur pointer is much better than stone_coder. im just a newbie with this co'z i just started with cpp, i use vb. thanks again gape

- 12-04-2001swoopy
Try this:

Code:`//check if input is a binary number`

int validinput(char *pbin)

{

for(int x = 0; x < 8; x++)

{

if (pbin[x] != '0' && pbin[x] != '1')

return 0;

}

if (pbin[8] != '\0')

return 0;

return 1;

}

- 12-04-2001GaPe
Unregistered, no problem.

- 12-04-2001CheeseMonkeywhy does that work?
Can somebody explain how that function that swoopy posted works? I was thinking about trying that, but i didn't because i thought it was asking if a certain character was '1' and '2' and a single character can't be both things.

Oh yeah, thanks alot swoopy, everything works great now. - 12-05-2001CheeseMonkeynevermind
i'm an idiot, i was sitting in school today and thought about it and realized why it worked.

- 12-08-2001Unregistered
I'm a different Unregistered... Who saw a similar post on another message board,

http://www.cpp-home.com/forums/read....&i=1454&t=1454

see what you might glean from it if you like