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

This is a discussion on *decimal to binary, decimal to hexadecimal and vice versa* within the **C++ Programming** forums, part of the General Programming Boards category; anybody knows how to convert a decimal into a binary, decimal to hexadecimal, binary to hexadecimal and decimal, hexadecimal to ...

- 12-02-2001 #1UnregisteredGuest
## 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

- 12-02-2001 #2anybody 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

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

- 12-02-2001 #3

- Join Date
- Nov 2001
- Posts
- 669

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)

- 12-02-2001 #4

- Join Date
- Dec 2001
- Posts
- 9

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-2001 #5UnregisteredGuest
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-2001 #6

- Join Date
- Oct 2001
- Posts
- 2,934

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-2001 #7

- Join Date
- Nov 2001
- Posts
- 669

Unregistered, no problem.

Current projects:

1) User Interface Development Kit (C++)

2) HTML SDK (C++)

3) Classes (C++)

4) INI Editor (Delphi)

- 12-04-2001 #8

- Join Date
- Dec 2001
- Posts
- 9

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

- 12-05-2001 #9

- Join Date
- Dec 2001
- Posts
- 9

## nevermind

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

- 12-08-2001 #10UnregisteredGuest
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

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011