{"id":21571145,"url":"https://github.com/cheprasov/js-base-converter","last_synced_at":"2025-04-10T14:20:32.498Z","repository":{"id":57101423,"uuid":"145249817","full_name":"cheprasov/js-base-converter","owner":"cheprasov","description":"The library allows to convert a number between arbitrary bases.","archived":false,"fork":false,"pushed_at":"2021-06-10T19:42:51.000Z","size":17,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-17T21:12:19.810Z","etag":null,"topics":["binary","converter","decimal","hexadecimal","octal"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cheprasov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"cheprasov","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2018-08-18T20:11:53.000Z","updated_at":"2024-05-31T19:17:17.000Z","dependencies_parsed_at":"2022-08-20T22:40:33.126Z","dependency_job_id":null,"html_url":"https://github.com/cheprasov/js-base-converter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fjs-base-converter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fjs-base-converter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fjs-base-converter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fjs-base-converter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cheprasov","download_url":"https://codeload.github.com/cheprasov/js-base-converter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248233935,"owners_count":21069493,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary","converter","decimal","hexadecimal","octal"],"created_at":"2024-11-24T11:15:00.910Z","updated_at":"2025-04-10T14:20:32.474Z","avatar_url":"https://github.com/cheprasov.png","language":"JavaScript","funding_links":["https://github.com/sponsors/cheprasov"],"categories":[],"sub_categories":[],"readme":"[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)\n\n@cheprasov/base-converter\n=========\n\nThe library allows to convert a number between arbitrary bases.\n\n## Note\nBefore using the library please check native method [`Number.toString([radix])`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString), maybe this is what you need.\nThe library was written mostly for converting more complicated or custom bases.\n\n## Quick examples:\n```javascript\n\nimport { BaseConverter, baseBinary, baseHexadecimal } from '@cheprasov/base-converter';\n\n// encode\n\nconsole.log('7 dec =\u003e', BaseConverter.encode(7, baseBinary), 'bin');\n// 7 dec =\u003e 111 bin\n\nconsole.log('255 dec =\u003e', BaseConverter.encode(255, baseHexadecimal), 'hex');\n// 255 dec =\u003e FF hex\n\nconsole.log('255 dec =\u003e', BaseConverter.encode(255, ['A', 'B', 'C']), 'custom');\n// 255 dec =\u003e BAABBA custom\n\n// decode\n\nconsole.log('101 bin =\u003e', BaseConverter.decode(101, baseBinary), 'dec');\n// 101 bin =\u003e 5 dec\n\nconsole.log('FA hex =\u003e', BaseConverter.decode('FA', baseHexadecimal), 'dec');\n// FA hex =\u003e 250 dec\n\nconsole.log('BAA custom =\u003e', BaseConverter.decode('BAA', ['A', 'B', 'C']), 'dec');\n// BAA custom =\u003e 9 dec\n\n// convert\n\nconsole.log('1010 bin =\u003e', BaseConverter.convert(1010, baseBinary, baseHexadecimal), 'hex');\n// 1010 bin =\u003e A hex\n\nconsole.log('FF hex =\u003e', BaseConverter.convert('FF', baseHexadecimal, baseBinary), 'bin');\n// FF hex =\u003e 11111111 bin\n\nconsole.log('FF custom =\u003e', BaseConverter.convert('FF', ['A', 'B', 'C', 'D', 'E', 'F'], ['1', '2', '3']), 'custom');\n// FF custom =\u003e 2133 bin\n```\n\n## How to install\n\n```bash\n\u003e npm install @cheprasov/base-converter\n```\n\n## Documentation\n\n### Import\n```javascript\n// import library\nimport { BaseConverter } from '@cheprasov/base-converter';\n\n// import preset bases\nimport {\n    baseBinary,\n    baseOctal,\n    baseDecimal,\n    baseHexadecimal,\n    base62\n} from '@cheprasov/base-converter';\n```\n\n### Custom base\nA base has type `Array\u003cstring|number\u003e`, it means that each base is array of integers or strings. Also, each element is the array should have 1 symbol only.\n\nExamples:\n```javascript\n\n// correct\nconst base4 = [0, 1, 2, 3];\nconst base8 = [0, 1, 2, 3, 'W', 'X', 'Y', 'Z'];\nconst baseSome = ['0', 'a', 'A', 'b', 'B'];\nconst baseSymbols = ['!', '@', '#', '$', '%', '^', '\u0026', '*', '.', '-', '+', '='];\n\n// wrong\nconst base4 = [0, 10, 20, 30]; // should be 1 symbols for each element\nconst base8 = [0, 1, 2, 3, 'AA', 'BB', 'CC', 'DD']; // should be 1 symbols for each element\nconst baseSome = ['0', '1', '1', '2', '2']; // duplicates of items\nconst baseSome = ['A']; // should be more then 1 element in the array\n```\n\n### Public methods\n\n#### `encode(value: number, base: Array\u003cstring | number\u003e): string | null`\nConverts decimal number to provided base. Will return `null` if value can not be converted.\n\nExample:\n```javascript\nconsole.log('7 dec =\u003e', BaseConverter.encode(7, baseBinary), 'bin');\n// 7 dec =\u003e 111 bin\n\nconsole.log('255 dec =\u003e', BaseConverter.encode(255, baseHexadecimal), 'hex');\n// 255 dec =\u003e FF hex\n\nconsole.log('255 dec =\u003e', BaseConverter.encode(255, ['A', 'B', 'C']), 'custom');\n// 255 dec =\u003e BAABBA custom\n```\n\n#### `decode(value: string, base: Array\u003cstring | number\u003e): number | null`\nConverts value from provided base to decimal number. Will return `null` if value can not be converted.\n\nExample:\n```javascript\nconsole.log('101 bin =\u003e', BaseConverter.decode(101, baseBinary), 'dec');\n// 101 bin =\u003e 5 dec\n\nconsole.log('FA hex =\u003e', BaseConverter.decode('FA', baseHexadecimal), 'dec');\n// FA hex =\u003e 250 dec\n\nconsole.log('BAA custom =\u003e', BaseConverter.decode('BAA', ['A', 'B', 'C']), 'dec');\n// BAA custom =\u003e 9 dec\n```\n\n#### `convert(value: string | number, fromBase: BaseType, toBase: BaseType): string | null`\nConverts value from one base to another base. Will return `null` if value can not be converted.\n\n```javascript\nconsole.log('1010 bin =\u003e', BaseConverter.convert(1010, baseBinary, baseHexadecimal), 'hex');\n// 1010 bin =\u003e A hex\n\nconsole.log('FF hex =\u003e', BaseConverter.convert('FF', baseHexadecimal, baseBinary), 'bin');\n// FF hex =\u003e 11111111 bin\n\nconsole.log('FF custom =\u003e', BaseConverter.convert('FF', ['A', 'B', 'C', 'D', 'E', 'F'], ['1', '2', '3']), 'custom');\n// FF custom =\u003e 2133 bin\n```\n\n## Something does not work\n\nFeel free to fork project, fix bugs, tests and finally request for pull\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheprasov%2Fjs-base-converter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcheprasov%2Fjs-base-converter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheprasov%2Fjs-base-converter/lists"}