Daniel B. answered 10/28/21
PhD in Computer Science with 42 years in Computer Research
The answer depends on how big the numbers are.
If they are very big, then the problem is difficult.
Imagine a number containing millions of digits.
After seeing only a small number of digits in one radix, say radix 2,
we need to produce the leading digit in the other radix, say radix 10.
I do not know how to solve that.
So I will just show you a pseudocode for the simple problem, where
the numbers involved fit into a machine representable integer.
So below are two pieces of pseudocode:
One converts a string of digits of any given radix (2 or 10) into an integer k.
And the other converts an integer k into a string of digits in any given radix.
In both cases all integers are non-negative.
Negative integers are yet another story, called "two's complement".
PSEUDOCODE 1:
# Convert an array A of length n with digits in radix r into an integer k.
# A[0] contains the least significant digit
# A[n-1] contains the most significant digit
k = 0 # the result k will keep accumulating the final value
i = 0 # start iterating from the least significant digit
v = 1 # always v= ri is the contribution of a digit in position i
loop for i = 0 ... n-1
k += A[i]*v # k gets the contribution of digit i
v *= r # v becomes the value of the next more significant position
PSEUDOCODE 2:
# Convert a given non-negative integer k into an array A with digits in radix r.
# We assume that A has been allocated big enough.
# The pseudocode will compute
# n -- the number of digits in radix r needed to represent k
# A -- the digits of the representation
# A[0] will contains the least significant digit
# A[n-1] will contains the most significant digit
n = 0 # start iterating from the least significant digit
loop until break
A[n] = k % r # each digit is the remainder of k if divided by r
n += 1 # prepare for next digit
k /= r # integer division discarding remainder
if (k == 0) # the whole number k converted
then break from the loop