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
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
Yes most of us here!anybody knows how to convert a decimal into a binary, decimal to hexadecimal, binary to hexadecimal and decimal, hexadecimal to decimal and binary?
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.can you give me the code? pls
If you want more help than that then post your efforts.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
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.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
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
any help would be greatly appreciatedCode:#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; }
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
Try this:
Your bintodec() seems to work ok.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; }
Unregistered, no problem.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
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.
i'm an idiot, i was sitting in school today and thought about it and realized why it worked.
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