An open API service indexing awesome lists of open source software.

https://github.com/kaushalmodi/baser

Emacs-Lisp Library for converting to and from Decimal, Hexadecimal and Binary numbers
https://github.com/kaushalmodi/baser

binary conversion decimal emacs emacs-lisp hexadecimal numbers

Last synced: 17 days ago
JSON representation

Emacs-Lisp Library for converting to and from Decimal, Hexadecimal and Binary numbers

Awesome Lists containing this project

README

          

#+title: Emacs-Lisp Library for converting to and from Decimal, Hexadecimal and Binary numbers
#+author: Kaushal Modi

[[https://github.com/kaushalmodi/baser/actions][https://github.com/kaushalmodi/baser/actions/workflows/test.yml/badge.svg]] [[https://www.gnu.org/licenses/gpl-3.0][https://img.shields.io/badge/License-GPL%20v3-blue.svg]]

* Installation
Clone this repo, have ~baser.el~ in the ~load-path~ and ~(require
'baser)~.
* General Usage
** Interactive
Functions called using ~M-x~ or key bindings.
*** With region selected
1. Select a region in which you want to convert all the numbers.
2. Run ~~ interactively.
*** Without region selected
1. Run ~~ interactively.
2. Type the number you want to convert.
** Non-interactive
Call ~eval-expression~ (bound to ~M-:~ by default) and type the elisp
expression of the conversion function.

- Examples ::
#+begin_src emacs-lisp
(baser-dec-to-hex 100) ;; => "00000064"
(baser-dec-to-hex "8'd55") ;; => "37"
(baser-dec-to-hex "-8'd55") ;; => "c9"

(baser-hex-to-dec "0x100") ;; => 256
(baser-hex-to-dec "12'hfff") ;; => -1
(baser-hex-to-dec "16'hfff") ;; => 4095

(baser-hex-to-bin "0xabc") ;; => "1010_1011_1100"

(baser-bin-to-hex "4'b1101") ;; => "d"

(baser-dec-to-bin "4'd3") ;; => "0011"
(baser-dec-to-bin "-8'd3") ;; => "1111_1101"

(baser-bin-to-dec "1111") ;; => 15
(baser-bin-to-dec "4'b1111") ;; => -1
(baser-bin-to-dec "5'b1111") ;; => 15
#+end_src
* Conversions
|-----------------------+-------------+--------------------|
| From Base | To Base | Function |
|-----------------------+-------------+--------------------|
| 10 (number or string) | 16 (string) | ~baser-dec-to-hex~ |
| 16 (string) | 10 (number) | ~baser-hex-to-dec~ |
| 16 (string) | 2 (string) | ~baser-hex-to-bin~ |
| 2 (string) | 16 (string) | ~baser-bin-to-hex~ |
| 10 (number or string) | 2 (string) | ~baser-dec-to-bin~ |
| 2 (string) | 10 (number) | ~baser-bin-to-dec~ |
|-----------------------+-------------+--------------------|
* Development
** Running Tests
*** Run all tests
#+begin_src shell
make test
#+end_src
*** Run tests matching a specific string
Run ~make test MATCH=~. For example, to run all tests where
the name matches "hex-to-dec" completely or partially, run:

#+begin_src shell
make test MATCH=hex-to-dec
#+end_src