top of page

Number Base Converter

by XR_XharpRazor

deals with any positive bases (larger than 1, larger than 36),
4 conversions at once

How does this work ?

Assume that we have an InArray with a base of InBase

We want to convert it into OutArray with a base of OutBase

Here, each Array represents a positive integer
where the elements represents the multiple of a power of its base

​

for example

3x4^0

1x4^1

2x4^2

0x4^3

2x4^4

2x4^5

base 4 = 2607

To get things prepared, let's get 2 extra arrays for refference : InBase^Index and OutBase^Index

​

```

InBase^Index = [InBase^0 , InBase^1 , InBase^2 , InBase^3 , ... , InBase^Index ]

OutBase^Index = [OutBase^0 , OutBase^1 , OutBase^2 , OutBase^3 ]

```

​

In this example, we have

1

4

16

64

256

1024

1

7

49

343

Here, our InArray only has 6 elements, so 6 elements for the InBase^Index Array

note that 7^3 (=343) < 1024 < 7^4 (=2401)​

At this point,

our OutArray is a bunch of zeros where the length is the same as OutBase^Index here

Assume that we have 2 pointers, pointing at the last element of the 2 arrays : 

​

```

PointerI = InArray.length - 1

PointerJ = OutArray.length - 1

```

PointerI

3

1

2

0

2

2

0

0

0

0

PointerJ

While PointerI is pointing at something that is not 0,

we will try to "distribute" what I is pointing into sum of powers of OutBases

​

Here's a before :

3

1

2

0

2

2

0

0

0

0

Here's the after :

3

1

2

0

2

1

2

6

6

2

Note that 1024 = 2 x 7^3 + 6 x 7^2 + 6 x 7^1 + 2 x 7^0

​

we have to distribute it again until what PointerI pointing is 0

PointerI

3

1

2

0

2

0

4

12

12

4

Once PointerI is pointing at a 0, PointerI can move to the next slot,

Note that

PointerJ is there to help with the distribution, which will be reset once a round of distribution is done

PointerI is there to help keep track of the entire progress

​

Repeat this whole thing until PointerI is -1,

in this case, our OutArray will end up with

23

18

22

4

base 7 = 2607

To some extend, this is correct, but we can clean up a little bit

one key point is that

One Key Point In Base Conversion

 

when decreasing a digit , say the Base^N place by an amount of Base,

the Base^(N+1) place will increase by one

​

or vice versa, meaning

​

when increasing a digit , say the Base^N place , by 1

the Base^(N-1) place will decrease by Base

...

x+b

...

x

...

x-b

y-1

...

y

...

y+1

...

another way to understand it is

either we can package up or bundle up a number of things and throw it to a higher place

or we can unpack or extract a package and throw it to a lower place

bottom of page