My Dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

89 lines
1.4 KiB

  1. #!/usr/bin/env bash
  2. ##Helper Functions
  3. urlencodespecial() {
  4. # urlencode <string>
  5. old_lc_collate=$LC_COLLATE
  6. LC_COLLATE=C
  7. local length="${#1}"
  8. for (( i = 0; i < length; i++ )); do
  9. local c="${1:i:1}"
  10. case $c in
  11. [a-zA-Z0-9.~_-]) printf "$c" ;;
  12. *) printf '%%%02X' "'$c" ;;
  13. esac
  14. done
  15. LC_COLLATE=$old_lc_collate
  16. }
  17. urlencodeall() {
  18. # urlencode <string>
  19. old_lc_collate=$LC_COLLATE
  20. LC_COLLATE=C
  21. local length="${#1}"
  22. for (( i = 0; i < length; i++ )); do
  23. local c="${1:i:1}"
  24. printf '%%%02X' "'$c"
  25. done
  26. LC_COLLATE=$old_lc_collate
  27. }
  28. string="$1"
  29. if [ -z "$string" ]; then
  30. string="$(cat)"
  31. fi
  32. # Base 64
  33. echo -en "Base64\t"
  34. echo "$string" | base64 --wrap=0
  35. echo ""
  36. #URL
  37. echo -en "URL\t"
  38. urlencodespecial "$string"
  39. echo ""
  40. #URL All
  41. echo -en "URL All\t"
  42. urlencodeall "$string"
  43. echo ""
  44. #Hex Encode
  45. echo -en "Hex\t"
  46. hex=$( echo -n "$string" | xxd -ps | sed 's/\([A-Fa-f0-9][A-Fa-f0-9]\)/\1 /g' | sed 's/ $//')
  47. echo "$hex"
  48. #Hex Encode With 0x
  49. echo -en "Hex\t"
  50. echo -n "0x$hex" | sed -e 's/ / 0x/g'
  51. echo
  52. #Decimal
  53. echo -en "Decimal\t"
  54. #for i in $(echo -n "$hex" ); do
  55. for i in $hex; do
  56. echo "ibase=16;$i" | bc
  57. echo
  58. done | tr '\n' ' '
  59. echo
  60. #Octal
  61. echo -en "Octal\t"
  62. for i in $hex; do
  63. echo "ibase=16;obase=8;$i" | bc
  64. done | tr '\n' ' '
  65. echo
  66. #Binary
  67. echo -en "Binary\t"
  68. for i in $hex; do
  69. echo "ibase=16;obase=2;$i" | bc | awk '{printf "%08d", $1}'
  70. echo
  71. done | tr '\n' ' '
  72. echo