Explore BrainMass

Explore BrainMass

    Conversion of Binary number to Decimal number

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Write a C++ program that converts eight (8) bit binary numbers to their decimal equivalents. The program should retrieve the binary numbers (one at a time) from a data file (use a long int for the variable that represents the binary number).
    For each binary number, convert the number to decimal one bit at a time (or all at once depending on your algorithm). The program will read the binary number (eight binary digits on one line with no spaces between them), convert the number, then go back to the data file to retrieve the next number. The number will have leading 0's if the binary number is smaller than eight digits. For example, the binary number could be 00001100 which would make the result decimal number 12. Or the number could be 10000001 and would result in decimal number 129. Note that the first example number had leading 0's.

    The name of the input data file should be "binary.dat". For testing purposes, you will need to create this file and add some valid (and invalid, if any) binary numbers using the criteria described below.

    For 5 extra credit points, you should make sure that the binary number only has 0's or 1's and your program should go on to the next number if not. However, you can always assume that the user will always enter eight digits (no more, no less.)

    This program could have multiple solutions. The conversion of binary numbers to decimal numbers will be explained in class. However, you can look up conversions on the Internet for more examples. You may (although it isn't the only solution) need to use a combination of the modulus and division operators. It is advisable to plan out the solution to this problem well in advance of coding the program.

    You will need to use a for() loop to process the eight bits. You will need to use a while() loop to access the numbers in the data file (checking for the end of file). This program will help you read data from a data file, valid the data and process correct numbers.

    © BrainMass Inc. brainmass.com March 6, 2023, 2:16 pm ad1c9bdddf
    https://brainmass.com/computer-science/cpp/conversion-of-binary-number-to-decimal-number-79472

    Solution Preview

    Solution:
    Conversion begins by taking each digit from the binary numbers' leftmost bit
    position (Most Significat Bit) and multiplying it with its positional value
    e.g. if [ a7 a6 a5 a4 a3 a2 a1 a0 ] is a string representing binary numbers
    then, its final decimal equivalent ...

    Solution Summary

    I have attached both the C++ program and the executable. The detailed comments and solution algorithm will help you understand and implement your own version as well.

    $2.49

    ADVERTISEMENT