Guest User

Untitled

a guest
Jul 16th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #! /bin/bash
  2. # 16 juli 2018 by Hendriyawan (gdev)
  3. # thanks to : stackoverflow
  4. # program bash untuk konversi text
  5. # text ke ascii
  6. # text ke hexadesimal
  7. # text ke octal
  8.  
  9. # ini method / fungsi ubah text ke ascii
  10. text_to_ascii(){
  11. words=$1
  12. ascii_code=() #ini list dalam pemrograman bash
  13. for ((i=0; i < ${#words}; i++)); do
  14. ascii=$(printf %d \'${words:$i:1})
  15. #menambahkan ke list
  16. ascii_code+=($ascii)
  17. done
  18. echo ""
  19. echo -e "ASCII : \033[1;32m${ascii_code[@]}\033[0m"
  20. echo ""
  21. # [@] = artinya menampilkan seluruh item list
  22. }
  23.  
  24. # ini method / fungsi ubah text ke hexadecimal
  25. text_to_hex(){
  26. words=$1
  27. hexacode=() #ini list dalam pemrograman bash
  28. for ((i=0; i < ${#words}; i++)); do
  29. hexadecimal=$(printf %X \'${words:$i:1})
  30. hexacode+=($hexadecimal)
  31. done
  32. echo ""
  33. echo -e "HEXADECIMAL : \033[1;32m${hexacode[@]}\033[0m"
  34. echo ""
  35. # [@] = artinya menampilkan seluruh item list
  36. }
  37.  
  38. # ini method / fungsi ubah text ke octal
  39. text_to_octal(){
  40. words=$1
  41. octal_code=()
  42. for ((i=0; i < ${#words}; i++)); do
  43. octal=$(printf %o \'${words:$i:1})
  44. octal_code+=($octal)
  45. done
  46. echo ""
  47. echo -e "OCTAL : \033[1;32m${octal_code[@]}\033[0m"
  48. echo ""
  49. }
  50.  
  51. options=$1
  52. words=$2
  53. if [ -z $options ] || [ -z $words ]; then
  54. echo "gunakan : texttool [options] kata/kalimat !"
  55. echo "options are :"
  56. echo "-tta\t ubah text ke ascii"
  57. echo "-tth\t ubah text ke hexadecimal"
  58. echo ""
  59. exit 1
  60. elif [ $options == "-tta" ]; then
  61. text_to_ascii $words
  62. elif [ $options == "-tth" ]; then
  63. text_to_hex $words
  64. elif [ $options == "-tto" ]; then
  65. text_to_octal $words
  66. fi
Add Comment
Please, Sign In to add comment