1. ## decimal 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

2. anybody knows how to convert a decimal into a binary, decimal to hexadecimal, binary to hexadecimal and decimal, hexadecimal to decimal and binary?
Yes most of us here!
can you give me the code? pls
No. But as you said please I'll give you some pointers. Look into sprintf() or stringstreams. The binary conversions you can do with shifts and bitwise operators.
If you want more help than that then post your efforts.

3. 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.

4. 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;
}```
any help would be greatly appreciated

5. 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

6. 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;
}```
Your bintodec() seems to work ok.

7. Unregistered, no problem.

8. ## why 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.

9. ## nevermind

i'm an idiot, i was sitting in school today and thought about it and realized why it worked.

10. I'm a different Unregistered... Who saw a similar post on another message board,