Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create AnyBase #1534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
imrahulkb wants to merge 10 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from imrahulkb:patch-6
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Conversions/ConvertBase
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const microConvert = (n, base1, base2) => {
let converted = 0;
let multiplier = 1;
while (n) {
converted += (n % base2) * multiplier;
multiplier *= base1;
n = Math.floor(n / base2);
}
return converted;
};

const convertBase = (num, frombase, tobase) => {
return microConvert(microConvert(num, frombase, 10), 10, tobase);
};

export { convertBase };
41 changes: 41 additions & 0 deletions Conversions/test/ConvertBase.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { convertBase } from '../ConvertBase'

test('Converting number 10010 from base 4 to base 10 should be 260', () => {
const res = convertBase(10010, 4, 10)
expect(res).toBe(260)
})

test('Converting number 1101 from base 2 to base 10 should be 13', () => {
const res = convertBase(1101, 2, 10)
expect(res).toBe(13)
})

test('Converting number 213 from base 5 to base 8 should be 53', () => {
const res = convertBase(213, 5, 8)
expect(res).toBe(72)
})

test('Converting number 37 from base 8 to base 3 should be 2111', () => {
const res = convertBase(37, 8, 3)
expect(res).toBe(1011)
})

test('Converting number 123 from base 6 to base 4 should be 33', () => {
const res = convertBase(123, 6, 4)
expect(res).toBe(303)
})

test('Converting number 1010 from base 2 to base 9 should be 74', () => {
const res = convertBase(1010, 2, 9)
expect(res).toBe(11)
})

test('Converting number 731 from base 7 to base 5 should be 2121', () => {
const res = convertBase(731, 7, 5)
expect(res).toBe(2430)
})

test('Converting number 345 from base 9 to base 10 should be 344', () => {
const res = convertBase(345, 9, 10)
expect(res).toBe(284)
})

AltStyle によって変換されたページ (->オリジナル) /