Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Use in terminal as command: sudo chmod +x file.sh | mv file.sh /bin/file
- # ---------- Conversion Functions ----------
- bin_to_ascii() {
- for bin in "$@"; do
- echo -n "$((2#$bin))" | awk '{printf "%c", $1}'
- done
- echo
- }
- ascii_to_bin() {
- input="$*"
- for (( i=0; i<${#input}; i++ )); do
- printf "%08d " "$(echo "obase=2; $(printf "%d" "'${input:$i:1}")" | bc)"
- done
- echo
- }
- bin_to_hex() {
- for bin in "$@"; do
- printf "%02X " "$((2#$bin))"
- done
- echo
- }
- bin_to_dec() {
- for bin in "$@"; do
- echo -n "$((2#$bin)) "
- done
- echo
- }
- dec_to_bin() {
- for dec in "$@"; do
- echo -n "$(echo "obase=2; $dec" | bc) "
- done
- echo
- }
- dec_to_ascii() {
- for dec in "$@"; do
- printf "%b" "$(printf "\\$(printf "%03o" "$dec")")"
- done
- echo
- }
- ascii_to_hex() {
- input="$*"
- for (( i=0; i<${#input}; i++ )); do
- printf "%02X " "'${input:$i:1}"
- done
- echo
- }
- ascii_to_dec() {
- input="$*"
- for (( i=0; i<${#input}; i++ )); do
- printf "%d " "'${input:$i:1}"
- done
- echo
- }
- hex_to_ascii() {
- for hex in "$@"; do
- printf "\x$hex"
- done
- echo
- }
- hex_to_bin() {
- for hex in "$@"; do
- dec=$((16#$hex))
- echo -n "$(echo "obase=2; $dec" | bc | awk '{printf "%08d", $0}') "
- done
- echo
- }
- hex_to_dec() {
- for hex in "$@"; do
- echo -n "$((16#$hex)) "
- done
- echo
- }
- dec_to_hex() {
- for dec in "$@"; do
- printf "%02X " "$dec"
- done
- echo
- }
- # ---------- Usage Info ----------
- print_usage() {
- echo "Usage: $0 -[conversion] [values...] [-r input_file] [-w output_file]"
- echo "Conversions:"
- echo " -ba Binary to ASCII"
- echo " -ab ASCII to Binary"
- echo " -bh Binary to Hex"
- echo " -bd Binary to Decimal"
- echo " -db Decimal to Binary"
- echo " -da Decimal to ASCII"
- echo " -ah ASCII to Hex"
- echo " -ad ASCII to Decimal"
- echo " -ha Hex to ASCII"
- echo " -hb Hex to Binary"
- echo " -hd Hex to Decimal"
- echo " -dh Decimal to Hex"
- echo " -r filename Convert file contents"
- echo " -w filename Write output to file"
- }
- # ---------- Argument Parsing ----------
- conversion=""
- output_file=""
- input_file=""
- values=()
- while [[ $# -gt 0 ]]; do
- case "$1" in
- -ba|-ab|-bh|-bd|-db|-da|-ah|-ad|-ha|-hb|-hd|-dh)
- conversion="$1"
- shift
- ;;
- -w)
- output_file="$2"
- shift 2
- ;;
- -r)
- input_file="$2"
- shift 2
- ;;
- *)
- values+=("$1")
- shift
- ;;
- esac
- done
- # ---------- Input Source ----------
- if [[ -n "$input_file" ]]; then
- if [[ ! -f "$input_file" ]]; then
- echo "Error: File '$input_file' not found."
- exit 1
- fi
- # Read file content into values array
- mapfile -t file_lines < "$input_file"
- file_content="${file_lines[*]}"
- # shellcheck disable=SC2206
- values=(${file_content})
- fi
- # ---------- Validate ----------
- if [[ -z "$conversion" || ${#values[@]} -eq 0 ]]; then
- print_usage
- exit 1
- fi
- # ---------- Perform Conversion ----------
- case $conversion in
- -ba) result=$(bin_to_ascii "${values[@]}") ;;
- -ab) result=$(ascii_to_bin "${values[@]}") ;;
- -bh) result=$(bin_to_hex "${values[@]}") ;;
- -bd) result=$(bin_to_dec "${values[@]}") ;;
- -db) result=$(dec_to_bin "${values[@]}") ;;
- -da) result=$(dec_to_ascii "${values[@]}") ;;
- -ah) result=$(ascii_to_hex "${values[@]}") ;;
- -ad) result=$(ascii_to_dec "${values[@]}") ;;
- -ha) result=$(hex_to_ascii "${values[@]}") ;;
- -hb) result=$(hex_to_bin "${values[@]}") ;;
- -hd) result=$(hex_to_dec "${values[@]}") ;;
- -dh) result=$(dec_to_hex "${values[@]}") ;;
- *)
- echo "Unsupported conversion: $conversion"
- print_usage
- exit 1
- ;;
- esac
- # ---------- Output ----------
- if [[ -n "$output_file" ]]; then
- echo "$result" > "$output_file"
- echo "Output written to $output_file"
- else
- echo "$result"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement