Advertisement

11.28.2003 at 05:06PM PST, ID: 20811309
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

converting binary to decimal

Tags: binary, decimal, c, convert
Before you ask.  This is school work, saying that I don't want the answer.  I would however like some help in solving my problem.  I know how to convert binary to decimal on paper.  Meaning I can do this.  EX: 11010 = 0 x 2^0 + 1 * 2^1 + 0 x 2^2 + 1 x 2^3 + 1 x 2^4 = 26.  I can do that and understand it fine.  here is what I am trying to figure out.

How to convert binary to decimal using integers.  I have figured out how to convert them by using charactor arrays.  I have also figured out how to convert from decimal to binary by using integers.  I have just not figured out how to go from binary to decimal using integers rather the arrays.
Why do I want to do it that way you might ask?  Well my professor said that the project would be easier that way.  In fact it would be b/c of the way the rest of the project is set up.  If I can figure out how to do it doing integers it would save me some time.  Points will be given on how much help you give and how revelant the post(s) is/are.  I will not accept anyone's post that post the answer.  
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: buckeyes33
Solution Provided By: Kdo
Participating Experts: 4
Solution Grade: A
Views: 178
Translate:
Loading Advertisement...
11.28.2003 at 05:10PM PST, ID: 9840720

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.28.2003 at 05:16PM PST, ID: 9840732

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.28.2003 at 07:12PM PST, ID: 9841074

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.28.2003 at 11:03PM PST, ID: 9841555

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.29.2003 at 06:40AM PST, ID: 9842324

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.29.2003 at 07:53AM PST, ID: 9842544

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.29.2003 at 09:58AM PST, ID: 9842899

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.29.2003 at 11:07AM PST, ID: 9843127

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.29.2003 at 05:47PM PST, ID: 9844144

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.01.2003 at 12:25PM PST, ID: 9852726

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.01.2003 at 12:33PM PST, ID: 9852788

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.01.2003 at 06:56PM PST, ID: 9855189

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.01.2003 at 08:51PM PST, ID: 9855725

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.01.2003 at 08:53PM PST, ID: 9855731

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.01.2003 at 09:26PM PST, ID: 9855859

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.01.2003 at 10:07PM PST, ID: 9856017

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.01.2003 at 10:11PM PST, ID: 9856033

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.01.2003 at 10:22PM PST, ID: 9856081

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.01.2003 at 11:13PM PST, ID: 9856288

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.02.2003 at 06:17AM PST, ID: 9858313

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.02.2003 at 04:11PM PST, ID: 9862983

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.02.2003 at 06:44PM PST, ID: 9863682

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.02.2003 at 06:54PM PST, ID: 9863725

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.02.2003 at 06:55PM PST, ID: 9863729

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.02.2003 at 10:01PM PST, ID: 9864518

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.02.2003 at 10:46PM PST, ID: 9864717

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.02.2003 at 10:48PM PST, ID: 9864725

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.02.2003 at 10:53PM PST, ID: 9864744

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.03.2003 at 12:06AM PST, ID: 9864995

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.03.2003 at 05:26PM PST, ID: 9871473

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.03.2003 at 10:39PM PST, ID: 9872665

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.04.2003 at 08:02PM PST, ID: 9879915

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.04.2003 at 08:04PM PST, ID: 9879925

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.04.2003 at 08:07PM PST, ID: 9879937

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.04.2003 at 08:07PM PST, ID: 9879939

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
11.28.2003 at 05:10PM PST, ID: 9840720
In the C programming language, an integer is converted from binary to decimal when the %d specifier is used in printf(), sprintf() or fprintf(). %o is used to convert a number to its octal representation, and %x is used to get the hex representation.
 
11.28.2003 at 05:16PM PST, ID: 9840732
 
11.28.2003 at 07:12PM PST, ID: 9841074

Rank: Sage

Hi Buckeyes33,

Converting values is a popular theme here on the boards.  One of the most common traits of these questions is that in asking to convert from binary to decimal, what the poster really wants (and needs) is to convert from binary to display (ASCII).  Your questions sounds like it fits into this category so I'm going to answer based on that inference.

First, all integer values are binary to the computer.  It doesn't matter whether you're dealing with int, short, or long values.  They are always binary, which is the computer's "native" representation.

You and I don't think in binary.  (Well, we can, and sometimes do, but it's not the way our minds have been trained to think.)  We do think in decimal.  Using your example above, if we were to attempt to purchase 26 items from the grocery store no one would think much of it.  But if we were to discuss the matter with a cashier and explain that the reason that we bought 11010 items is because their their 10 for 1 sale made us buy twice the number that we had intended so we bought 11010 instead of 1101, we probably wouldn't ever get the transaction completed accurately.

But that's the way computers "think".  We think in decimal so whenever we have our computer programs display a value it is usually converted to decimal display (ASCII).

Making the conversion is really easy.  You asked that the answer not be posted as a program (code).  I'll do my best to comply, though it's tougher this way.  The actual code is only about 6 lines of code.  (Well, 110 lines in binary.  :)  Here are the steps that you need to follow to generate your own conversion:

First create a character buffer for the assembly.  Initialize the entire buffer to blanks, except the last character which must be zero to terminate the string.

Then create a character pointer and point it to the LAST space in the string.

Using the % function, take the modulo 10 remainder of the value to be converted.  Using your sample value of 26 you will get 6.  Store the 6 in the location pointed to by the character pointer.  But you cannot store the binary 6, you must display the ASCII value for 6.  You can generate this by adding the binary value of 6 to the binary value of the ASCII 0.  (Value + '0'.)

Divide the value to be converted by 10.  If the new value is zero, the conversion is complete and the character pointer points to the converted value.  With our sample value of 26, of course the result is 2.

Decrement the character pointer so that it now points to the position where we will store the next digit.

Loop back up and take the next modulo 10 value.

That's it.  That's all there is to converting binary to decimal display (ASCII).

Hope that I understood the question correctly and gave you a response in the form that you need,
Kent
Accepted Solution
 
11.28.2003 at 11:03PM PST, ID: 9841555

Rank: Sage

>I have just not figured out how to go from binary to decimal using integers rather the arrays.

Kent:
>First create a character buffer for the assembly.
Looks like you missed it this time ;o)

Buckeyes33:
>I have just not figured out how to go from binary to decimal using integers rather the arrays.
This should be pretty simple ...
1. you know that binary number is nothing but a sequence of 0s and 1s .... to get equivalent decimal, each 0 and each 1 is multiplied by some weight depending on its position in the sequence...

2. you also know that & is the bitwise operator and ( number & 0x1 ) will give you the least significant bit of number

3. you also know that we have a right shift operator in C

putting 2 and 3 together, we can easily determine if LSB of a given binary number is 0 or 1 ... we can also right shift the number to examine the next bit and then next and so on ...

If I have 32 bit int then 32 right shifts in a loop will allow us to examine all the bits ... only problem is to determine the weight for each bit, multiply each bit by its weight and keep the cumulative sum... Since we are doing all manipulation in the loop, we can easily get the weight by initialzing weight to 1 and multiplying by 2 at each iteration ....

So we have:::
initialize weight to 1
initialize cumulative to 0
for all bits in number do
          calculate ( number & 0x1 )
          multiply this by weight
          add it to cumulative sum
          multiply weight by 2
          right shift number by one bit
done
your equivalent decimal is in cumulaive sum

Cheers!
Sunny
 
11.29.2003 at 06:40AM PST, ID: 9842324

Rank: Sage

Hi MR Sunny,  :)

I didn't miss it.  I'm assuming that the poster wants to convert the value to decimal before displaying it.  He strongly suggests that he does.  Using a buffer with a "current" pointer eliminates the array notation.  Also, we don't know where he wants to avoid array references.  It could be that he also wants to eliminate an array to convert the binary to ASCII.

Hi Buckeyes33,

There is another applicable way to accomplish this and it works for converting to any base (or even displaying the value in the ASCII equivalent of its binary value).  It's with recursion.  But if your current assignment is to convert binary to display, I doubt that the classroom discussion has covered much of recursion.  Still, here goes:

Create a function that will convert a single digit.  Let's call it DisplayAnyBase().  The function has two parameters:  The value to convert and the base to which it will be converted.

Within the function, perform the two basic conversion operations:  Take the modulo(value,base) with the % operator and then divide the original (passed) value by the base.

If the new value is non-zero, call the function again (from within the function) passing the new value and the base.  (Recursively, this will continue until a remainder of zero is found.

Now, (still coding within the function) display the value that was computed by the modulo(value,base) calculation.  If the base is 10 or less, you can simply add this value to '0' to get the ASCII (displayable) value.  If the base is greater than 10, for example 16 (hex), you'll need to convert values of 9 or less by adding '0'.  Values of 10 or greater will convert by adding 'A' and subtracting 10.  (Think about it a second....)

The function now does a return and if there are more digits to display the previous iteration of the function will display the next digit.

Since recursion is probably new to you, here's a diagram of what happens.  No code!  :)   To start with, we have to understand that all of these are equivalent:

DisplayAnyValue (125, 10);
DisplayAnyValue (0x7D, 10);
DisplayAnyValue (0175, 10);
DisplayAnyValue (bin(1111101), 10);

They all pass the same value for the first argument.  The compiler takes care of converting the value into its machine equivalent.  But we're people, not machines, so let's use the top one.  It will make the demonstration the easiest.

Value Passed    Base   Saved Digit   New Value
  125                 10           5                12
   12                  10           2                1
    1                   10           1                0


Look at the "Saved Digit" column.  What you see is the individual digits of the original value, but in reverse order!  (The original value of 125 produced the digits 5, 2, and 1.)  You'll find that this occurs in almost every conversion method that you'll employ.  This is why the character buffer method in my first post had you decrement the pointer -- you're essentially moving right to left.  It's also why the recursion method works like it does.  It picks off the digits one-by-one and only when the last digit is found does it go about displaying them.

Good Luck,
Kent
 
11.29.2003 at 07:53AM PST, ID: 9842544
I have to say that sunnycoder has the correct assumption.  I want to convert from binary to decimal.  I don't want to print the ACSI euivilant to the binary number.

For one part of my project I have to ask the user to input a binary number, two of them actually.  I then have to convert them to decimal, add them, and then convert them back to binary and then print the binary.

When I get some time I will try to figure it out using the method sunny has given.That is the only problem I am having.  Unless someone knows how to make sure that I binary is actually a binary number.  I have to test for a binary number that the user has entered.  If the number entered is not binary then I have to ask the user again for the binary numbers.  by doing this I am going to use a do-while loop and put the user prompts inside the do-while loo and then test to see if the number entered is binary.

thanks for the help.
 
11.29.2003 at 09:58AM PST, ID: 9842899

Rank: Sage

Hi Buckeyes,

> I want to convert from binary to decimal.  

The computer keeps integers in binary form.  Whether it's base 2, 3, 10, 16 or anything else doesn't matter.  The computer knows what it's internal representation is and how to use it.  (More on this later -- it's the underlying theme of the question.)

> I don't want to print the ACSI euivilant to the binary number.

Sure you do.  If you want to put an answer on the screen, it must be converted to a displayable value.  You do this by converting the binary value of each digit (which is the machine readable form) to the ASCII equivalent (which is the human readable form).

To test this, try this simple program.  (This code isn't the answer to your original question, just a demonstration of how binary and ASCII differ.)

#include <stdlib.h>

int BinaryValue = 1;

main ()
{
  puts ("Binary 1 = ");
  putch (BinaryValue);
  putch ("\n");       /* new line  */

  puts ("Display 1 = ");
  putch (BinaryValue + '0');
  putch ("\n");
}

> I have to ask the user to input a binary number, two of them actually.

The user inputs strings of ASCII characters!  You then convert the ASCII characters to binary, the machine's "native" representation.

>  I then have to convert them to decimal.

After you ask for a value and the user enters one, you have an ASCII string!  Pretend for a second that the User entered 1010.  If we think of 1010 as a binary value then it has an equivalent value of 10 decimal (base 10).  But what you'l find is that the actual bit stream that your C program has stored is 0011000100110000001100010011000000000000, the binary equivalent of the character string "1010", which is 0x3130313000.

This character string must be converted to it machine form equivalent before the computer can use it.  We must convert the string "1010" to the 4-bit value 1010.

> and then convert them back to binary and then print the binary.

Nope.  You convert them to ASCII before you print them.  They are already in binary.  Go back to the short program 2 bullets back.


Part of the confusion here is terminology.  You're using the term "binary" to mean "base 2".  Granted, one definition of binary IS "base 2", but the most common definition (within computer circles) is the machine readable form of a number that can be used in native calculations such as add, subtract, and multiply.

So going back to the original problem, here are the steps that you'll need to program:

1)  Display a nice message for the user that he is to input a binary value.  (In this case, the term "binary" is used to tell the user that the value must contain only 1s and 0s.)

2)  Read the input value.  (It is in the form of a string!!!!!)

3)  Convert the string to its machine readable equivalent and store it in an int.

4)  Repeat steps 1 through 3 for the second value.

5)  Add the two int values.

6)  Convert the answer back to a characters string of 1s and 0s.

7)  Display the string created in step 6.

Kent
 
11.29.2003 at 11:07AM PST, ID: 9843127
I think that you might both be correct.  It is just two different ways of doing it.  I don't have time to look into it and probably won't for two days, so be patient please.  Your help is very much appriecated.


to get rid of the lack of confussion of what I am trying to do here is a link.  It is part 2 of the link.
http://web.ics.purdue.edu/~cs158/proj/p4.pdf
 
11.29.2003 at 05:47PM PST, ID: 9844144

Rank: Sage


I love it when a plan comes together....  :)

From my previous examples, everything still holds true EXCEPT that you will not need (or be allowed to) convert the string to/from binary!

So here's the scoop:

1)  Display a nice message for the user that he is to input a binary value.  (In this case, the term "binary" is used to tell the user that the value must contain only 1s and 0s.)

2)  Read the input value into a string.  Note that you are going to want to scan the string backwards.  You can do this by counting characters, or by setting the first character of the string to 0 and starting the input at the second character.  I strongly recommend that you choose the second option.  (You also need to check the string to make sure that all characters are "0" or "1".)

You are going to scan the string backwards because that is the way we add we usuallyl add so that we can keep track of the carry with the least amount of overhead.  In a normal (base 10) addition, you might see something like this:

  8094
+  236
--------

The first thing that you'll do is add the 4 and the 6, generating 10.  Drop the 0 and carry the 1.  You next add 1, 9, and 3 generating 13.  Drop the 3 and ca