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

  • Home
  • SEARCH
  • 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 8565963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:32:56+00:00 2026-06-11T17:32:56+00:00

I am trying to implement an algorithm to convert an array of integers, which

  • 0

I am trying to implement an algorithm to convert an array of integers, which represent the digits of the fractional part of a number, from one base to another. In other words:

int[] input = {0, 0, 1, 0, 1}; // 0.00101 in base 2
int[] output = convertBase(input, 2, 10, 5); // convertBase(input, fromBase, toBase, precision)
output == {1, 5, 6, 2, 5}; // .15625 in base 10

There is a suggested algorithm, which is worded as such:

for (i < precisionB):

  1. Keep a carry, initialize to 0.
  2. From RIGHT to LEFT

    a. x = multiply the ith digit by baseB and add the carry

    b. the new ith digit is x % baseA

    c. carry = x / baseA

  3. output[i] = carry

But when I implement this, the second digit is always off by a bit for arrays that are longer than 3 digits. For the above example, it’ll return {1, 3, 6, 2, 5}. An input of {0, 1} in base 2 will properly return {2, 5} in base 10.

I don’t think I am properly understanding 2b. It seems like you are already done with the ith digit in the input array, replacing it shouldn’t matter?

Here is my code:

public static int[] convertBase(int[] digits, int baseA,
                                int baseB, int precisionB) {
    if (baseA < 2 | baseB < 2 | precisionB < 1) {
        return null;
    }
    int[] input = digits.clone();
    int[] output = new int[precisionB];
    int carry = 0;
    int j;
    int x;

    for (int i = 1; i <= precisionB; i++) {
        j = precisionB - i;
        if (input[j] >= baseA | input[j] < 0) {
            return null;
        }
        x = (input[j] * baseB) + carry;
        input[j] = x % baseA;
        carry = x / baseA;
        output[j] = carry;
    }

    return output;
}

This is from MIT’s 6.005 course, problem set 1.

  • 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-06-11T17:32:57+00:00Added an answer on June 11, 2026 at 5:32 pm

    I eventually figured it out. The solution is that you must loop over all digits of the input array for each digit of the output. Here’s the code in Java:

    public static int[] convertBase(int[] digits, int baseA,
                                    int baseB, int precisionB) {
        if (baseA < 2 | baseB < 2 | precisionB < 1) {
            return null;
        }
        int[] input = digits.clone();
        int[] output = new int[precisionB];
        int carry;
        int j;
        int x;
    
        for (int i = 1; i <= precisionB; i++) {
            carry = 0;
            for (int k = 0; k < input.length; k++) {
                j = input.length - 1 - k;
                if (input[j] >= baseA | input[j] < 0) {
                    return null;
                }
                x = (input[j] * baseB) + carry;
                input[j] = x % baseA;
                carry = x / baseA;
            }
            output[i-1] = carry;
        }
        return output;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement this algorithm description from a previous question I had here
i am trying to implement discrete curve evolution algorithm in c++ do any one
I'm trying to implement a force base graph layout algorithm, based on http://en.wikipedia.org/wiki/Force-based_algorithms_(graph_drawing )
I am trying to implement a vision algorithm, which includes a prefiltering stage with
i'm trying to implement an algorithm which is flood-fill alike. the problem is that
I'm trying to implement a bicubic interpolation algorithm to reconstruct higher-resolution data from a
I've been trying to implement the algorithm from wikipedia and while it's never outputting
i am trying to implement following algorithm: suppose there is array a[3][3], my aim
I'm trying to implement Maximum Rectangle Algorithm from Dr. Dobbs (Listing four) with Python.
I am trying to implement the eigenface algorithm using OpenCV in VS2010. I am

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.