Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6121133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:44:58+00:00 2026-05-23T15:44:58+00:00

I am currently working on a basic program which converts a binary number to

  • 0

I am currently working on a basic program which converts a binary number to an octal. Its task is to print a table with all the numbers between 0-256, with their binary, octal and hexadecimal equivalent. The task requires me only to use my own code (i.e. using loops etc and not in-built functions). The code I have made (it is quite messy at the moment) is as following (this is only a snippit):

        int counter = ceil(log10(fabs(binaryValue)+1));
        int iter;
        if (counter%3 == 0)
        {
            iter = counter/3;
        }
        else if (counter%3 != 0)
        {
            iter = ceil((counter/3)); 
        }
        c = binaryValue;
        for (int h = 0; h < iter; h++)
        {
            tempOctal = c%1000;
            c /= 1000;
            int count = ceil(log10(fabs(tempOctal)+1));
            for (int counter = 0; counter < count; counter++)
            {
                if (tempOctal%10 != 0)
                {
                   e = pow(2.0, counter);
                   tempDecimal += e;
                }
                tempOctal /= 10;
            }
            octalValue += (tempDecimal * pow(10.0, h));
        }

The output is completely wrong. When for example the binary code is 1111 (decimal value 15), it outputs 7. I can understand why this happens (the last three digits in the binary number, 111, is 7 in decimal format), but can’t be able to identify the problem in the code. Any ideas?

Edit: After some debugging and testing I figured the answer.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    while (true)
{
    int binaryValue, c, tempOctal, tempDecimal, octalValue = 0, e;
    cout << "Enter a binary number to convert to octal: ";
    cin >> binaryValue;
    int counter = ceil(log10(binaryValue+1));
    cout << "Counter " << counter << endl;
    int iter;
    if (counter%3 == 0)
    {
       iter = counter/3;
    }
    else if (counter%3 != 0)
    {
       iter = (counter/3)+1; 
    }
    cout << "Iterations " << iter << endl;
    c = binaryValue;
    cout << "C " << c << endl;
    for (int h = 0; h < iter; h++)
    {
        tempOctal = c%1000;
        cout << "3 digit binary part " << tempOctal << endl;
        int count = ceil(log10(tempOctal+1));
        cout << "Digits " << count << endl;
        tempDecimal = 0;
        for (int counterr = 0; counterr < count; counterr++)
        {
            if (tempOctal%10 != 0)
            {
                 e = pow(2.0, counterr);
                 tempDecimal += e;
                 cout << "Temp Decimal value 0-7 " << tempDecimal << endl;
            }
            tempOctal /= 10;
        }
        octalValue += (tempDecimal * pow(10.0, h));
        cout << "Octal Value " << octalValue << endl;
        c /= 1000;
    }
cout << "Final Octal Value: " << octalValue << endl;
}
system("pause");
return 0;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:44:58+00:00Added an answer on May 23, 2026 at 3:44 pm

    It don’t understand your code; it seems far too complicated. But one
    thing is sure, if you are converting an internal representation into
    octal, you’re going to have to divide by 8 somewhere, and do a % 8
    somewhere. And I don’t see them. On the other hand, I see a both
    operations with both 10 and 1000, neither of which should be present.

    For starters, you might want to write a simple function which converts
    a value (preferably an unsigned of some type—get unsigned
    right before worrying about the sign) to a string using any base, e.g.:

    //! \pre
    //!     base >= 2 && base < 36
    //!
    //! Digits are 0-9, then A-Z.
    std::string convert(unsigned value, unsigned base);
    

    This shouldn’t take more than about 5 or 6 lines of code. But attention,
    the normal algorithm generates the digits in reverse order: if you’re
    using std::string, the simplest solution is to push_back each digit,
    then call std::reverse at the end, before returning it. Otherwise: a
    C style char[] works well, provided that you make it large enough.
    (sizeof(unsigned) * CHAR_BITS + 2 is more than enough, even for
    signed, and even with a '\0' at the end, which you won’t need if you
    return a string.) Just initialize the pointer to buffer +
    sizeof(buffer)
    , and pre-decrement each time you insert a digit. To
    construct the string you return:
    std::string( pointer, buffer + sizeof(buffer) ) should do the trick.

    As for the loop, the end condition could simply be value == 0.
    (You’ll be dividing value by base each time through, so you’re
    guaranteed to reach this condition.) If you use a do ... while,
    rather than just a while, you’re also guaranteed at least one digit in
    the output.

    (It would have been a lot easier for me to just post the code, but since
    this is obviously homework, I think it better to just give indications
    concerning what needs to be done.)

    Edit: I’ve added my implementation, and some comments on your new
    code:

    First for the comments: there’s a very misleading prompt: “Enter a
    binary number” sounds like the user should enter binary; if you’re
    reading into an int, the value input should be decimal. And there are
    still the % 1000 and / 1000 and % 10 and / 10 that I don’t
    understand. Whatever you’re doing, it can’t be right if there’s no %
    8
    and / 8. Try it: input "128", for example, and see what you get.

    If you’re trying to input binary, then you really have to input a
    string, and parse it yourself.

    My code for the conversion itself would be:

    //! \pre
    //!     base >= 2 && base <= 36
    //!
    //! Digits are 0-9, then A-Z.
    std::string toString( unsigned value, unsigned base )
    {
        assert( base >= 2 && base <= 36 );
        static char const digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char buffer[sizeof(unsigned) * CHAR_BIT];
        char* dst = buffer + sizeof(buffer);
        do
        {
            *--dst = digits[value % base];
            value /= base;
        } while (value != 0);
        return std::string(dst, buffer + sizeof(buffer));
    }
    

    If you want to parse input (e.g. for binary), then something like the
    following should do the trick:

    unsigned fromString( std::string const& value, unsigned base )
    {
        assert( base >= 2 && base <= 36 );
        static char const digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        unsigned results = 0;
        for (std::string::const_iterator iter = value.begin();
                iter != value.end();
                ++ iter)
        {
            unsigned digit = std::find
                ( digits, digits + sizeof(digits) - 1,
                  toupper(static_cast<unsigned char>( *iter ) ) ) - digits;
            if ( digit >= base )
                throw std::runtime_error( "Illegal character" );
            if ( results >= UINT_MAX / base
                 && (results > UINT_MAX / base || digit > UINT_MAX % base) )
                throw std::runtime_error( "Overflow" );
            results = base * results + digit;
        }
        return results;
    }
    

    It’s more complicated than toString because it has to handle all sorts
    of possible error conditions. It’s also still probably simpler than you
    need; you probably want to trim blanks, etc., as well (or even ignore
    them: entering 01000000 is more error prone than 0100 0000).

    (Also, the end iterator for find has a - 1 because of the trailing
    '\0' the compiler inserts into digits.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a basic send and receive program using UDP in
Im currently working with an API which requires we send our collection details in
Im currently working on a Python/Pygame module to wrap some basic sprite animation. Animation
I'm occurring some trouble writing a basic webserver in JAVA. It's currently working fine
In my current program, i'm accessing a HTTP Basic authenticated page like this, which
I'm working on an assembler for a very basic ISA. Currently I'm implementing parser
i'm a beginner programmer and i'm currently working on a basic navigation app. I
I'm currently working in .NET 2.0 Visual Basic. The current project is an Active
I'm currently working on a custom UI engine for a game. I've very basic
I'm working on a very basic scheduled FTP program, but I don't want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.