Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork41
🔢 Bits, bytes and address calculator
License
jarun/bcal
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
bcal (Byte CALculator) is a REPL CLI utility for storage expression (e.g."(2GiB * 2) / (2KiB >> 2)") evaluation, SI/IEC conversion, byte address calculation, base conversion and LBA/CHS calculation. It's very useful for those who deal with bits, bytes, addresses and binary prefixes frequently.
It has abc mode for general-purpose numerical calculations. Alternatively, it can also invoke the more featuredcalc which works better with expressions involving multiple bases.
bcal usesSI and IEC binary prefixes and supports 64-bit Operating Systems only.
- REPL and single execution modes
- evaluate arithmetic expressions involving storage units
- perform general purpose calculations (using bc or calc)
- works with piped input or file redirection
- convert to IEC/SI standard data storage units
- interactive mode with the last valid result stored for reuse
- show the address in bytes
- show address as LBA:OFFSET
- convert CHS to LBA andvice versa
- base conversion to binary, decimal and hex
- custom sector size, max heads/cylinder and max sectors/track
- minimal dependencies
bcal is written in C and depends on standard libc and GNU Readline (orBSD Editline). It invokes GNUbc orcalc for non-storage expressions.
To usecalc:
export BCAL_USE_CALC=1Installbcal from your package manager. If the version available is dated try an alternative installation method.
If you have git installed, clone this repository. Otherwise, download thelatest stable release ordevelopment version (risky).
Install to default location (/usr/local):
$ sudo make strip installTo link to libedit:
$ sudo make O_EL=1 strip installTo uninstall, run:
$ sudo make uninstallPREFIX is supported, in case you want to install to a different location.
bcal can be compiled and installed from source in the Termux environment onaarch64 Android devices. Instructions:
$ wget https://github.com/jarun/bcal/archive/master.zip$ unzip bcal-master.zip$ cd bcal-master/$ pkg install make clang readline-dev$ make strip installusage: bcal [-c N] [-f loc] [-s bytes] [expr] [N [unit]] [-b [expr]] [-m] [-d] [-h]Storage expression calculator.positional arguments: expr expression in decimal/hex operands N [unit] capacity in B/KiB/MiB/GiB/TiB/kB/MB/GB/TB https://en.wikipedia.org/wiki/Binary_prefix default unit is B (byte), case is ignored N can be decimal or '0x' prefixed hex valueoptional arguments: -c N show +ve integer N in binary, decimal, hex -f loc convert CHS to LBA or LBA to CHS refer to the operational notes in man page -s bytes sector size [default 512] -b [expr] enter bc mode or evaluate expression in bc -m show minimal output (e.g. decimal bytes) -d enable debug information and logs -h show this helpprompt keys: b toggle bc mode r show result from last operation s show sizes of storage types ? show prompt help q/double ↵ quit program- Interactive mode:
bcalenters the REPL mode if no arguments are provided. Storage unit conversion, base conversion and expression evaluation are supported in this mode. The last valid result is stored in the variabler. - Expression: Expression passed as argument in one-shot mode must be within double quotes. Inner spaces are ignored. Supported operators:
+,-,*,/,%and C bitwise operators (except~due to storage width dependency). - N [unit]:
Ncan be a decimal or '0x' prefixed hex value.unitcan be B/KiB/MiB/GiB/TiB/kB/MB/GB/TB. Default is Byte. As all of these tokens are unique,unitis case-insensitive. - Numeric representation: Decimal and hex are recognized in expressions and unit conversions. Binary is also recognized in other operations.
- Syntax: Prefix hex inputs with
0x, binary inputs with0b. - Precision: 128 bits if
__uint128_tis available or 64 bits for numerical conversions. Floating point operations uselong double. Negative values in storage expressions are unsupported. Only 64-bit operating systems are supported. - Fractional bytes do not exist because they can't be addressed.
bcalshows the floor value of non-integerbytes. - CHS and LBA syntax:
- LBA:
lLBA-MAX_HEAD-MAX_SECTOR[NOTE: LBA starts withl(case ignored)] - CHS:
cC-H-S-MAX_HEAD-MAX_SECTOR[NOTE: CHS starts withc(case ignored)] - Format conversion arguments must be hyphen separated.
- Any unspecified value, including the one preceding the first
-to the one following the last-, is considered0(zero). - Examples:
c-50--0x12--> C = 0, H = 50, S = 0, MH = 0x12, MS = 0l50-0x12-> LBA = 50, MH = 0x12, MS = 0
- LBA:
- Default values:
- sector size: 0x200 (512)
- max heads per cylinder: 0x10 (16)
- max sectors per track: 0x3f (63)
- bc variables:
scale= 10,ibase= 10.ris synced and can be used in expressions.bcis not called in minimal output mode.
Evaluate arithmetic expression of storage units.
$ bcal "(5kb+2mb)/3"$ bcal "5 tb / 12"$ bcal "2.5mb*3"$ bcal "(2giB * 2) / (2kib >> 2)"Convert storage capacity to other units and get address, LBA.
$ bcal 20140115 b$ bcal 0x1335053 B$ bcal 0xaabbcc kb$ bcal 0xdef GibNote that the units are case-insensitive.
Convert storage capacity, set sector size to 4096 to calculate LBA.
$ bcal 0xaabbcc kb -s 4096Convert LBA to CHS.
$ bcal -f l500$ bcal -f l0x600-18-0x7e$ bcal -f l0x300-0x12-0x7eConvert CHS to LBA.
$ bcal -f c10-10-10$ bcal -f c0x10-0x10-0x10$ bcal -f c0x10-10-2-0x12$ bcal -f c-10-2-0x12$ bcal -f c0x10-10--0x12Show binary, decimal and hex representations of a number.
$ bcal -c 20140115$ bcal -c 0b1001100110101000001010011$ bcal -c 0x1335053bcal> c 20140115 // Interactive modeInvoke
bc.$ bcal -b '3.5 * 2.1 + 5.7'bcal> b // Interactive modebc vars: scale = 10, ibase = 10, last = rbc> 3.5 * 2.1 + 5.7Pipe input.
$ printf '15 kib + 15 gib \n r / 5' | bcal -m$ printf '15 + 15 + 2' | bcal -bmRedirect from file.
$ cat expr15 gib + 15 kibr / 5$ bcal -m < exprUse as a general-purpose calculator.
$ bcal -b
Due to the nature of the project, it's extremely important to test existing functionality before raising any PR.bcal has several test cases written intest.py. To execute the test cases locally, installpytest and run:
$ make$ python3 -m pytest test.pyCopyright © 2016Arun Prakash Jana
About
🔢 Bits, bytes and address calculator
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors9
Uh oh!
There was an error while loading.Please reload this page.