Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Ekim_Gram

Binary to Decimal and vice versa (C++)

This topic is 5389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here''s my goal: I want to make a program in C++ that converts decimal numbers to binary and vice versa. i.e. [input] 1101 [output] 11 [input] 11 [output] 1101 How would I go about making a binary -> decimal table and then using it to output binary/decimal numbers? I was thinking of having them input a string of binary numbers i.e. 1101 and then using the strlen function would make a table. Like if strlen of String is 4, then you would have an int of some sort called n. n=1 then if strlen(String) > 2 n+1 and then n*n until strlen(String) = 0; Of couse I''d have a temp variable like strlen(String) = stringLength. so if stringLength > 2, n=1, stringLength--, n*n, stringLength-- until stringLength = 0. Then I would put the results of n*(n) into an array and then do some sort of division to put out the binary form. For those of you who don''t know the binary table, it looks like this:
256 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1
----------------------------------------
    |     |    |    |    | 1 | 1 | 0 | 1

1101 = 11
11 = 1001
If it''s unclear, I''m sorry but I''m majorly confused about this. Any tips would be greatly appreciated!
R.I.P. Mark Osback Solo Pa Mi Gente VG-Force

Share this post


Link to post
Share on other sites
Advertisement
Deci->Bin

ex: 33

(divided by two : carry)
33
16 : 1
8 : 0
4 : 0
2 : 0
1 : 0

The result being (top to bottom) 100001

Bin->Deci

1 0 0 0 0 1

(1 * (2^0)) + (0 * (2^1)) + (0 * (2^2)) + (0 * (2^3)) + (0 * (2^4)) + (1 * (2^5)) EDIT: though obviously the algo should be reading the binary string from right to left or whatever to compensate

If you don't get it feel free to say so.

[edited by - kordova on October 15, 2003 8:57:56 PM]

Share this post


Link to post
Share on other sites

char binary[8]

int decimal
Get set of 8 binary numbers, save in array
while index < 8
decimal += array[index] * 2 ^ index //if array[index] is 0

//doesn't add anything

Get next set of digits
When no digits are left, print decimal

Then reverse that for decimal to binary.


Edit: Remember to mulitply the decimal number if you have more than one string of binary numbers.
[edited by - Aerolithe on October 15, 2003 9:13:07 PM]

[edited by - Aerolithe on October 15, 2003 9:13:45 PM]

[edited by - Aerolithe on October 15, 2003 9:15:43 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!