Advertisement
xzlui

[BASH] ASCII/Hex/Decimal/Binary Converter

May 25th, 2025 (edited)
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.19 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Use in terminal as command: sudo chmod +x file.sh | mv file.sh /bin/file
  4.  
  5. # ---------- Conversion Functions ----------
  6.  
  7. bin_to_ascii() {
  8.     for bin in "$@"; do
  9.         echo -n "$((2#$bin))" | awk '{printf "%c", $1}'
  10.     done
  11.     echo
  12. }
  13.  
  14. ascii_to_bin() {
  15.     input="$*"
  16.     for (( i=0; i<${#input}; i++ )); do
  17.         printf "%08d " "$(echo "obase=2; $(printf "%d" "'${input:$i:1}")" | bc)"
  18.    done
  19.    echo
  20. }
  21.  
  22. bin_to_hex() {
  23.    for bin in "$@"; do
  24.        printf "%02X " "$((2#$bin))"
  25.     done
  26.     echo
  27. }
  28.  
  29. bin_to_dec() {
  30.     for bin in "$@"; do
  31.         echo -n "$((2#$bin)) "
  32.     done
  33.     echo
  34. }
  35.  
  36. dec_to_bin() {
  37.     for dec in "$@"; do
  38.         echo -n "$(echo "obase=2; $dec" | bc) "
  39.     done
  40.     echo
  41. }
  42.  
  43. dec_to_ascii() {
  44.     for dec in "$@"; do
  45.         printf "%b" "$(printf "\\$(printf "%03o" "$dec")")"
  46.    done
  47.    echo
  48. }
  49.  
  50. ascii_to_hex() {
  51.    input="$*"
  52.    for (( i=0; i<${#input}; i++ )); do
  53.        printf "%02X " "'${input:$i:1}"
  54.    done
  55.    echo
  56. }
  57.  
  58. ascii_to_dec() {
  59.    input="$*"
  60.    for (( i=0; i<${#input}; i++ )); do
  61.        printf "%d " "'${input:$i:1}"
  62.    done
  63.    echo
  64. }
  65.  
  66. hex_to_ascii() {
  67.    for hex in "$@"; do
  68.        printf "\x$hex"
  69.    done
  70.    echo
  71. }
  72.  
  73. hex_to_bin() {
  74.    for hex in "$@"; do
  75.        dec=$((16#$hex))
  76.        echo -n "$(echo "obase=2; $dec" | bc | awk '{printf "%08d", $0}') "
  77.    done
  78.    echo
  79. }
  80.  
  81. hex_to_dec() {
  82.    for hex in "$@"; do
  83.        echo -n "$((16#$hex)) "
  84.     done
  85.     echo
  86. }
  87.  
  88. dec_to_hex() {
  89.     for dec in "$@"; do
  90.         printf "%02X " "$dec"
  91.     done
  92.     echo
  93. }
  94.  
  95. # ---------- Usage Info ----------
  96. print_usage() {
  97.     echo "Usage: $0 -[conversion] [values...] [-r input_file] [-w output_file]"
  98.     echo "Conversions:"
  99.     echo "  -ba  Binary to ASCII"
  100.     echo "  -ab  ASCII to Binary"
  101.     echo "  -bh  Binary to Hex"
  102.     echo "  -bd  Binary to Decimal"
  103.     echo "  -db  Decimal to Binary"
  104.     echo "  -da  Decimal to ASCII"
  105.     echo "  -ah  ASCII to Hex"
  106.     echo "  -ad  ASCII to Decimal"
  107.     echo "  -ha  Hex to ASCII"
  108.     echo "  -hb  Hex to Binary"
  109.     echo "  -hd  Hex to Decimal"
  110.     echo "  -dh  Decimal to Hex"
  111.     echo "  -r filename   Convert file contents"
  112.     echo "  -w filename   Write output to file"
  113. }
  114.  
  115. # ---------- Argument Parsing ----------
  116.  
  117. conversion=""
  118. output_file=""
  119. input_file=""
  120. values=()
  121.  
  122. while [[ $# -gt 0 ]]; do
  123.     case "$1" in
  124.         -ba|-ab|-bh|-bd|-db|-da|-ah|-ad|-ha|-hb|-hd|-dh)
  125.             conversion="$1"
  126.             shift
  127.             ;;
  128.         -w)
  129.             output_file="$2"
  130.             shift 2
  131.             ;;
  132.         -r)
  133.             input_file="$2"
  134.             shift 2
  135.             ;;
  136.         *)
  137.             values+=("$1")
  138.             shift
  139.             ;;
  140.     esac
  141. done
  142.  
  143. # ---------- Input Source ----------
  144. if [[ -n "$input_file" ]]; then
  145.     if [[ ! -f "$input_file" ]]; then
  146.         echo "Error: File '$input_file' not found."
  147.         exit 1
  148.     fi
  149.     # Read file content into values array
  150.     mapfile -t file_lines < "$input_file"
  151.     file_content="${file_lines[*]}"
  152.     # shellcheck disable=SC2206
  153.     values=(${file_content})
  154. fi
  155.  
  156. # ---------- Validate ----------
  157. if [[ -z "$conversion" || ${#values[@]} -eq 0 ]]; then
  158.     print_usage
  159.     exit 1
  160. fi
  161.  
  162. # ---------- Perform Conversion ----------
  163. case $conversion in
  164.     -ba) result=$(bin_to_ascii "${values[@]}") ;;
  165.     -ab) result=$(ascii_to_bin "${values[@]}") ;;
  166.     -bh) result=$(bin_to_hex "${values[@]}") ;;
  167.     -bd) result=$(bin_to_dec "${values[@]}") ;;
  168.     -db) result=$(dec_to_bin "${values[@]}") ;;
  169.     -da) result=$(dec_to_ascii "${values[@]}") ;;
  170.     -ah) result=$(ascii_to_hex "${values[@]}") ;;
  171.     -ad) result=$(ascii_to_dec "${values[@]}") ;;
  172.     -ha) result=$(hex_to_ascii "${values[@]}") ;;
  173.     -hb) result=$(hex_to_bin "${values[@]}") ;;
  174.     -hd) result=$(hex_to_dec "${values[@]}") ;;
  175.     -dh) result=$(dec_to_hex "${values[@]}") ;;
  176.     *)
  177.         echo "Unsupported conversion: $conversion"
  178.         print_usage
  179.         exit 1
  180.         ;;
  181. esac
  182.  
  183. # ---------- Output ----------
  184. if [[ -n "$output_file" ]]; then
  185.     echo "$result" > "$output_file"
  186.     echo "Output written to $output_file"
  187. else
  188.     echo "$result"
  189. fi
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement