Skip to content

javascript-utilities/decimal-to-base

Repository files navigation

Decimal To Base

Converts decimal to another base, eg. hex, octal, or binary

Byte size of Decimal To Base Open Issues Open Pull Requests Latest commits decimal-to-base Demos



Requirements

NodeJS development dependencies may be installed via NPM...

npm install

Note, installing dependencies are only required for development, ie. this project is ready to use as is.


Quick Start

Clone this project...

Linux/MacOS

mkdir -vp ~/git/hub/javascript-utilities

cd ~/git/hub/javascript-utilities

git clone [email protected]:javascript-utilities/decimal-to-base.git

Windows

set _organization_directory="%HOMEDRIVE%%HOMEPATH%\git\hub\javascript-utilities"

if not exists %_organization_directory (
  md %_organization_directory
)

CD /D %_organization_directory

git clone git@github.com:javascript-utilities/decimal-to-base.git

... then utilize the decimalToBase function within the decimal-to-base.js script within your application.


Usage

The JavaScript source code of this project is transpiled for ECMAScript version 6 (es2017) by default, so should be useful for either Web Browser or NodeJS applications.

NodeJS

If utilizing NPM to track project dependencies it is possible to install this project via...

npm install javascript-utilities/decimal-to-base

... Otherwise if not using NPM, then change directories to where this project was cloned, and start a NodeJS shell...

cd git/hub/javascript-utilities/decimal-to-base

node

Either import or require the decimalToBase function to preform conversions...

const decimalToBase = require('./decimal-to-base.js');

decimalToBase(32, 16);
//> 0x20

decimalToBase(15, 16);
//> 0xF

//...

GitHub Pages

For GitHub Pages it is recommended to utilize this project as a Git Submodule.

Change directories to the repository that will depend upon this project, and checkout the gh-pages branch...

cd git/hub/<name>/<repository>

git checkout gh-pages

Make a directory for Git Modules...

mkdir -vp 'assets/js/modules'

Add this project as a Git Submodule...

git submodule add -b 'main'\
  --name 'decimal-to-base'\
  'https://github.com/javascript-utilities/decimal-to-base.git'\
  'assets/js/modules/decimal-to-base'

Commit added Git Submodule...

git commit -F- <<'EOF'
:heavy_plus_sign: Adds javascript-utilities/decimal-to-base#1 dependency



**Adds**

- `.gitmodules` file, tracks other Git repository code utilized by this project

- `assets/js/modules/decimal-to-base` submodule, Git tracked dependency
EOF

Edit Your ReadMe File

Quick Start Section

Clone with the following to avoid incomplete downloads



    git clone --recurse-submodules <url-for-your-project>

Updates/Upgrades Section

Update/upgrade submodules via


    git submodule update --init --merge --recursive

Add script tag that sources the decimal-to-base.js script, eg...

<script type="text/javascript" src="assets/js/modules/decimal-to-base/decimal-to-base.js"></script>

Edit Your HTML

index.html (example)

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <title>Tests Decimal to Base</title>
    <meta charset="utf-8">

    <script type="text/javascript" src="assets/js/modules/decimal-to-base/decimal-to-base.js"></script>

    <script type="text/javascript">
      function updateOutput(_event) {
        const decimal = document.getElementById('decimal').value;
        const radix = document.getElementById('radix').value;
        const output_element = document.getElementById('output');

        let output_value = '';
        try {
          output_value = decimalToBase(decimal, radix)
        } catch (e) {
          if (e instanceof SyntaxError) {
            console.error(e);
          } else if (e instanceof RangeError) {
            console.error(e);
          } else {
            throw e;
          }
        }

        output_element.value = output_value;
      }

      window.addEventListener('load', (_event) => {
        document.getElementById('decimal').addEventListener('input', updateOutput);
        document.getElementById('radix').addEventListener('input', updateOutput);
      });
    </script>
  </head>


  <body>
    <div class="container">
      <span>Radix: </span>
      <input type="text" id="radix" value="16">
      <br>

      <span>Input: </span>
      <input type="text" id="decimal">
      <br>

      <span>Output: </span>
      <input id="output" type="text" readonly>
    </div>
  </body>
</html>

Commit and Push

git add index.html
git add .github/README.md

git commit -F- <<'EOF'
:coffee: Utilizes javascript/decimal-to-base submodule


**Edits**


- `README.md` file, documentation updates for submodules
EOF

... And congratulate yourself on not having to write something similar!


Decimal to Base Reference

The incantations that decimalToBase understands

Parameters

  • decimal {number|string}

  • radix {number|string} - default 16

  • verbose {boolean} - default false

  • symbols_list {string[]} - default [...'0123456789abcdefghijklmnopqrstuvwxyz']

Examples

decimalToBase(540, 16);
//> "0x21C"

Notes

This repository may not be feature complete and/or fully functional, Pull Requests that add features or fix bugs are certainly welcomed. For example this project converts integers only at this time, I.E. floating point numbers are not supported.

  • Fork this repository to an account that you have write permissions for.

  • Add remote for fork URL. The URL syntax is [email protected]:<NAME>/<REPO>.git...

cd ~/git/hub/javascript-utilities/decimal-to-base

git remote add fork [email protected]:<NAME>/decimal-to-base.git
  • Commit your changes and push to your fork, eg. to fix an issue...
cd ~/git/hub/javascript-utilities/decimal-to-base


git commit -F- <<'EOF'
:bug: Fixes #42 Issue


**Edits**


- `<SCRIPT-NAME>` script, fixes some bug reported in issue
EOF


git push fork main

Note, the -u option may be used to set fork as the default remote, eg. git push fork main however, this will also default the fork remote for pulling from too! Meaning that pulling updates from origin must be done explicitly, eg. git pull orgin main

  • Then on GitHub submit a Pull Request through the Web-UI, the URL syntax is https://github.com/<NAME>/<REPO>/pull/new/<BRANCH>

Note; to decrease the chances of your Pull Request needing modifications before being accepted, please check the dot-github repository for detailed contributing guidelines.

Notice, edits to source code should target .ts (TypeScript) files, because JavaScript (.js) files are overwritten during transpiling process.


Attribution


License

Documentation for converting decimal to another base, eg. hex, octal, or binary
Copyright (C) 2024 S0AndS0

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, version 3 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

For further details review full length version of AGPL-3.0 License.