PoshCode Archive  Artifact Content

Artifact 049c884dc10b8998fcfc437dc3c5af66274c72ce648b85dff735ce7ba0a3548f:

  • File hex2dec.ps1 — part of check-in [c00891f291] at 2018-06-10 13:43:01 on branch trunk — Old bash script… (user: greg zakharov size: 939)

# encoding: ascii
# api: bash
# title: hex2dec
# description: Old bash script…
# version: 0.1
# author: greg zakharov
# license: CC0
# x-poshcode-id: 4562
# x-archived: 2013-11-01T01:43:17
# x-published: 2013-10-26T16:45:00
#
#
#!/bin/bash

err="\033[1;31m"
res="\033[1;36m"
end="\033[0m"

if [ $# -eq 1 ]; then
   n=$1
   case $n in
      *[[:xdigit:]])
         if [[ ${n:0:2} =~ 0[xX] ]]; then
            echo -e $res$n' = '$(($n))$end
         elif [[ ${n:0:1} =~ [xX] ]]; then
            echo -e $res'0'$n' = '$((0$n))$end
         elif [[ ${n:0:1} =~ [[:digit:]] && ! $n =~ [[:alpha:]] ]]; then
            printf $res$n' = 0x%X\n'$end $n
         elif [[ ${n:0:1} =~ [[:xdigit:]] ]]; then
            echo -e $res'0x'$n' = '$((0x$n))$end
         else
            echo -e $err'=>err'$end
         fi;;
      *)
         echo -e $err'=>err'$end;;
   esac
else
   echo -e $err'=>err'$end
fi