bcal (Byte CALculator) is a command-line utility for storage, hardware and firmware developers who deal with storage-specific numerical calculations, expressions, unit conversions or address calculations frequently. If you are one and can't calculate the hex address offset for (512 - 16) MiB immediately, or the value when the 43rd bit of a 64-bit address is set, bcal is for you.
To perform non-storage general purpose calculations, there's a switch to invoke bc.
bcal follows Ubuntu's standard unit conversion and notation policy. Only 64-bit operating systems are supported.
Love smart and efficient utilities? Explore my repositories. Buy me a cup of coffee if they help you.
- evaluate arithmetic expressions involving storage units
- 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 and vice versa
- base conversion to binary, decimal and hex
- custom sector size, max heads/cylinder and max sectors/track
- perform general purpose calculations (using bc)
- minimal dependencies
bcal is written in C and depends on standard libc and libreadline. It invokes GNU bc for non-storage expressions.
- AUR (
yaourt -S bcal) - Debian (
apt-get install bcal) - Fedora (
dnf install bcal) - Homebrew (
brew install bcal) - NixOS (
nix-env -i bcal) - Ubuntu (
apt-get install bcal) - Ubuntu PPA (
apt-get install bcal) - Void Linux (
xbps-install -S bcal)
Packages for Arch Linux, CentOS, Debian, Fedora, OpenSUSE Leap and Ubuntu are available with the latest stable release.
If you have git installed, clone this repository. Otherwise, download the latest stable release or development version (risky).
Install to default location (/usr/local):
$ make
$ sudo make install
To uninstall, run:
$ sudo make uninstall
PREFIX is supported, in case you want to install to a different location.
bcal can be compiled and installed from source in the Termux environment on aarch64 Android devices. Instructions:
$ aria2c https://github.com/jarun/bcal/archive/master.zip
$ unzip bcal-master.zip
$ cd bcal-master/
$ pkg install make clang readline-dev
$ make strip install
usage: bcal [-c N] [-f FORMAT] [-s bytes] [expr]
[N [unit]] [-b [expr]] [-m] [-d] [-h]
Storage expression calculator.
positional arguments:
expr evaluate storage arithmetic expression
+, -, *, /, >>, << with decimal/hex operands
Examples:
bcal "(5kb+2mb)/3"
bcal "5 tb / 12"
bcal "2.5mb*3"
bcal "(2giB * 2) / (2kib >> 2)"
N [unit] capacity in B/KiB/MiB/GiB/TiB/kB/MB/GB/TB
see https://wiki.ubuntu.com/UnitsPolicy
default unit is B (byte), case is ignored
N can be decimal or '0x' prefixed hex value
optional arguments:
-c N show +ve integer N in binary, decimal, hex
-f FORMAT convert CHS to LBA or LBA to CHS
formats are hyphen-separated
LBA format:
starts with 'l':
lLBA-MAX_HEAD-MAX_SECTOR
CHS format:
starts with 'c':
cC-H-S-MAX_HEAD-MAX_SECTOR
omitted values are considered 0
FORMAT 'c-50--0x12-' denotes:
C = 0, H = 50, S = 0, MH = 0x12, MS = 0
FORMAT 'l50-0x12' denotes:
LBA = 50, MH = 0x12, MS = 0
default MAX_HEAD: 16, default MAX_SECTOR: 63
-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 help, storage sizes and exit
prompt 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 variable r. - Expression: Expression passed as argument in one-shot mode must be within double quotes. Inner spaces are ignored.
- N [unit]:
Ncan be a decimal or '0x' prefixed hex value.unitcan be B/KiB/MiB/GiB/TiB/kB/MB/GB/TB following Ubuntu policy. 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 arguments are unsupported. - Fractional bytes do not exist because they can't be addressed.
bcalshows the floor value of non-integer bytes. - 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).
- 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.lastandrare synced when togglingbcmode.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 4096 -
Convert LBA to CHS.
$ bcal -f l500 $ bcal -f l0x600-18-0x7e $ bcal -f l0x300-0x12-0x7e -
Convert 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--0x12 -
Show binary, decimal and hex representations of a number.
$ bcal -c 20140115 $ bcal -c 0b1001100110101000001010011 $ bcal -c 0x1335053 bcal> c 20140115 // Interactive mode -
Invoke
bc.$ bcal -b '3.5 * 2.1 + 5.7' bcal> b // Interactive mode bc vars: scale = 10, ibase = 10, last = r bc> 3.5 * 2.1 + 5.7 -
Help and additional information.
$ man bcal $ bcal -h
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 in test.py. To execute the test cases locally, install pytest and run:
$ make
$ python3 -m pytest test.py
Copyright © 2016-2018 Arun Prakash Jana
