From: git Date: Sun, 15 Feb 2026 12:27:28 +0000 (-0500) Subject: initial public commit X-Git-Url: https://git.datadissipation.net/?a=commitdiff_plain;h=9273efc1db99de5d04a969b03d990cf14c303f12;p=baseconv.git initial public commit --- 9273efc1db99de5d04a969b03d990cf14c303f12 diff --git a/tobin b/tobin new file mode 100755 index 0000000..d9e6830 --- /dev/null +++ b/tobin @@ -0,0 +1,77 @@ +#!/bin/sh + +conv() { + while [ $# -gt 0 ]; do + if printf "%s" "$1" | grep -qE '^[0-9]+$'; then + if [ "$m" -eq 0 ]; then + printf "Decimal %s → Binary: " "$1" + fi + printf "%s" "$p" + printf "obase=2; %s" "$1" | bc + + elif printf "%s" "$1" | grep -qE '^([0-9A-Fa-f]+|0x[0-9A-Fa-f]+)$'; then + if [ "$m" -eq 0 ]; then + printf "Hex %s → Binary: " "$1" + fi + printf "%s" "$p" + printf "ibase=16; obase=2; %s" "$(printf "%s" "$1" | sed 'y/abcdef/ABCDEF/; s/^0x//')" | bc + + else + printf "Error: '%s' is not a valid decimal or hexadecimal number\n" "$1" >&2 + stat=1 + + fi + shift + done + return "$stat" +} + + +main() { + if ! [ "$1" = "x" ] && ! [ -t 0 ]; then + cat | tr '\n' ' ' | xargs tobin "x" "$@" + return "$?" + + elif [ "$1" = "x" ]; then + shift + + fi + + if [ $# -eq 0 ]; then + printf "Error: No arguments provided\nusage: tobin [-mnp] number1 number2 ...\n" >&2 + return 2 + + fi + + stat=0 + m=0; n=0; p="" + while getopts "lmnp" flag; do + case "$flag" in + m) m=1; ;; + n) n=1; ;; + p) p="0b" ;; + *) printf "usage: tobin [-mnp] number1 number2 ...\n" >&2 ; return 2 ;; + esac + done + + shift $(( OPTIND - 1 )) + + t=$(conv "$@") + stat=$? + + if [ "$n" -eq 1 ] && [ "$m" -eq 1 ]; then + t=$(printf "%s" "$t" | tr '\n' ' ') + + elif [ "$n" -eq 1 ] && [ "$m" -eq 0 ]; then + t=$(printf "%s" "$t" | tr '\n' ';') + + else + t=$(printf "%s" "$t") + + fi + + printf "%s\n" "$t" + return "$stat" +} + +main "$@" diff --git a/todec b/todec new file mode 100755 index 0000000..d48311d --- /dev/null +++ b/todec @@ -0,0 +1,82 @@ +#!/bin/sh + +conv() { + while [ $# -gt 0 ]; do + if printf "%s" "$1" | grep -qE '^0b[0-1]+$'; then + if [ "$m" -eq 0 ]; then + printf "Binary %s → Decimal: " "$1" + fi + t=$(printf "%s" "$1" | sed 's/^0b//') + printf "ibase=2; %s / %s" "$t" "$k" | bc | tr -d '\n' && printf "%s\n" "$p" + + elif printf "%s" "$1" | grep -qE '^([0-9A-Fa-f]+|0x[0-9A-Fa-f]+)$'; then + if [ "$m" -eq 0 ]; then + printf "Hex %s → Decimal: " "$1" + fi + t=$(printf "%s" "$1" | sed 's/^0x//') + printf "ibase=16; %s / %X" "$(printf "%s" "$t" | sed 'y/abcdef/ABCDEF/')" "0b$k" | bc | tr -d '\n' && printf "%s\n" "$p" + + else + printf "Error: '%s' is not a valid decimal or hexadecimal number\n" "$1" >&2 + stat=1 + + fi + shift + done + return "$stat" +} + + +main() { + if ! [ "$1" = "x" ] && ! [ -t 0 ]; then + cat | tr '\n' ' ' | xargs todec "x" "$@" + return "$?" + + elif [ "$1" = "x" ]; then + shift + + fi + + if [ $# -eq 0 ]; then + printf "Error: No arguments provided\nusage: todec [-mn] number1 number2 ...\n" >&2 + return 2 + + fi + + stat=0 + k=1; m=0; n=0; p=''; + while getopts "bmnKMG" flag; do + case "$flag" in + m) m=1; ;; + n) n=1; ;; + # 1024 + K) k=10000000000; p='K'; ;; + # 1 048 576 + M) k=100000000000000000000; p='M'; ;; + # 1 073 741 824 + G) k=1000000000000000000000000000000; p='G'; ;; + *) printf "usage: todec [-mnKMG] number1 number2 ...\n" >&2 ; return 2 ;; + esac + done + + shift $(( OPTIND - 1 )) + + t=$(conv "$@") + stat=$? + + if [ "$n" -eq 1 ] && [ "$m" -eq 1 ]; then + t=$(printf "%s" "$t" | tr '\n' ' ') + + elif [ "$n" -eq 1 ] && [ "$m" -eq 0 ]; then + t=$(printf "%s" "$t" | tr '\n' ';') + + else + t=$(printf "%s" "$t") + + fi + + printf "%s\n" "$t" + return "$stat" +} + +main "$@" diff --git a/tohex b/tohex new file mode 100755 index 0000000..e272405 --- /dev/null +++ b/tohex @@ -0,0 +1,81 @@ +#!/bin/sh + +conv() { + while [ $# -gt 0 ]; do + if printf "%s" "$1" | grep -qE '^[0-9]+$'; then + if [ "$m" -eq 0 ]; then + printf "Decimal %s → Hex: " "$1" + fi + printf "%s" "$p" + printf "%X\n" "$1" + + elif printf "%s" "$1" | grep -qE '^0b[0-1]+$'; then + if [ "$m" -eq 0 ]; then + printf "Binary %s → Hex: " "$1" + fi + printf "%s" "$p" + printf "%X\n" "$1" + + else + printf "Error: '%s' is not a valid binary or decimal number\n" "$1" >&2 + stat=1 + + fi + shift + done + return "$stat" +} + + +main() { + if ! [ "$1" = "x" ] && ! [ -t 0 ]; then + cat | tr '\n' ' ' | xargs tohex "x" "$@" + return "$?" + + elif [ "$1" = "x" ]; then + shift + + fi + + if [ $# -eq 0 ]; then + printf "Error: No arguments provided\nusage: tohex [-lmnp] number1 number2 ...\n" >&2 + return 2 + + fi + + stat=0 + l=0; m=0; n=0; p="" + while getopts "lmnp" flag; do + case "$flag" in + l) l=1; ;; + m) m=1; ;; + n) n=1; ;; + p) p="0x" ;; + *) printf "usage: tohex [-lmnp] number1 number2 ...\n" >&2 ; return 2 ;; + esac + done + + shift $(( OPTIND - 1 )) + + t=$(conv "$@") + stat=$? + + if [ "$n" -eq 1 ] && [ "$m" -eq 1 ]; then + t=$(printf "%s" "$t" | tr '\n' ' ') + + elif [ "$n" -eq 1 ] && [ "$m" -eq 0 ]; then + t=$(printf "%s" "$t" | tr '\n' ';') + + else + t=$(printf "%s" "$t") + + fi + + if [ "$l" -eq 1 ]; then + t=$(printf "%s" "$t" | tr '[:upper:]' '[:lower:]') + fi + printf "%s\n" "$t" + return "$stat" +} + +main "$@"