Skip to content

Commit 2f3a84a

Browse files
committed
util: Functions for converting binary to decimal and back and alphabet print function
1 parent 1b4454f commit 2f3a84a

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

src/util/binary.sh

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
#
3+
# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE
4+
#
5+
# Util functions to manipulate binary numbers.
6+
7+
if [ -n "${BINARY_MOD:-}" ]; then return 0; fi
8+
readonly BINARY_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
9+
10+
. ${BINARY_MOD}/../lang/p.sh
11+
12+
13+
# ----------
14+
# Functions.
15+
16+
function binary_d2b() {
17+
# From decimal to binary.
18+
local ctx; is_ctx "${1}" && ctx="${1}" && shift
19+
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
20+
local -r n="${1}"
21+
shift 1 || { ctx_wn $ctx; return $EC; }
22+
23+
# TODO: check for errors.
24+
echo "obase=2; ${n}" | bc
25+
}
26+
27+
function binary_b2d() {
28+
# From binary to decimal.
29+
local ctx; is_ctx "${1}" && ctx="${1}" && shift
30+
[ $# -ne 1 ] && { ctx_wn $ctx; return $EC; }
31+
local -r n="${1}"
32+
shift 1 || { ctx_wn $ctx; return $EC; }
33+
34+
# TODO: check for errors.
35+
echo "ibase=2;obase=A; ${n}" | bc
36+
}

src/util/binary_test.sh

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
#
3+
# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE
4+
#
5+
# Unit tests for the binary functions.
6+
7+
if [ -n "${BINARY_TEST_MOD:-}" ]; then return 0; fi
8+
readonly BINARY_TEST_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
9+
10+
. ${BINARY_TEST_MOD}/binary.sh
11+
. ${BINARY_TEST_MOD}/../testing/bunit.sh
12+
13+
14+
# ----------
15+
# Functions.
16+
17+
function test_binary_d2b() {
18+
local res
19+
20+
res=$(binary_d2b "3")
21+
[ "${res}" = "11" ] || \
22+
assert_fail
23+
24+
res=$(binary_d2b "33")
25+
[ "${res}" = "100001" ] || \
26+
assert_fail
27+
28+
return 0
29+
}
30+
readonly -f test_binary_d2b
31+
32+
function test_binary_b2d() {
33+
local res
34+
35+
res=$(binary_b2d "011")
36+
[ "${res}" = "3" ] || \
37+
assert_fail
38+
39+
res=$(binary_b2d "100001")
40+
[ "${res}" = "33" ] || \
41+
assert_fail
42+
43+
#binary_b2d "100001a" && \
44+
#assert_fail
45+
46+
return 0
47+
}
48+
readonly -f test_binary_b2d

src/util/char.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
#
3+
# https://github.com/EngineeringSoftware/gobash/blob/main/LICENSE
4+
#
5+
# Util char functions.
6+
7+
if [ -n "${CHAR_MOD:-}" ]; then return 0; fi
8+
readonly CHAR_MOD=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
9+
10+
. ${CHAR_MOD}/../lang/p.sh
11+
12+
13+
# ----------
14+
# Functions.
15+
16+
function char_alphabet() {
17+
# Print alphabet on a single line.
18+
local ctx; is_ctx "${1}" && ctx="${1}" && shift
19+
[ $# -ne 0 ] && { ctx_wn $ctx; return $EC; }
20+
shift 0 || { ctx_wn $ctx; return $EC; }
21+
22+
echo {a..z}
23+
}

src/util/p.sh

+2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ readonly UTIL_PACKAGE=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
1414
. ${UTIL_PACKAGE}/math.sh
1515
. ${UTIL_PACKAGE}/rand.sh
1616
. ${UTIL_PACKAGE}/strings.sh
17+
. ${UTIL_PACKAGE}/char.sh
1718
. ${UTIL_PACKAGE}/regexp.sh
1819
. ${UTIL_PACKAGE}/flags.sh
1920
. ${UTIL_PACKAGE}/complex.sh
2021
. ${UTIL_PACKAGE}/user.sh
22+
. ${UTIL_PACKAGE}/binary.sh

0 commit comments

Comments
 (0)