need help in expanding hexa decimal character range

Hi all,

I have a input like this

3AF9:3B01

and need to expand to the below output

3AF9
3AFA
3AFB
3AFC
3AFD
3AFE
3AFF
3B00
3B01

Please let me know the easiest way for achieving this. Thanks for the help in advance...

perl -e'
  @range = map hex, split /:/, shift;
  printf "%X\n", $_ for $range[0] .. $range[1];
  ' 3AF9:3B01
1 Like

A solution with awk:

echo "3AF9:3B01" | awk '
{
  split($0,a,":")
  for(i=hex2dec(a[1]);i<=hex2dec(a[2]);i++){
    printf("%X\n", i)
  }
}
function hex2dec(h,i,x,v){
  h=tolower(h);sub(/^0x/,"",h)
  for(i=1;i<=length(h);++i){
    x=index("0123456789abcdef",substr(h,i,1))
    v=(16*v)+x-1
  }
  return v
}'
1 Like

Thanks for the replies guys...

a=3AF9 b=3B01

# POSIX compliant
echo "ibase=obase=16; a=$a; b=$b; while (a<=b) a++" | bc

# Not POSIX
jot -w '%X' - 0x$a 0x$b 1

Regards,
Alister

more shorter, need gawk.

echo "3AF9:3B01" | awk -F : ' {for (i=strtonum("0x" $1);i<=strtonum("0x" $2);i++) printf("%X\n", i)}'

Using ksh93 ...

#!/bin/ksh93

x=3AF9:3B01

typeset -i16 a=0x${x%:*}
typeset -i16 b=0x${x#*:}

for (( ; a <= b; a++ ))
do
   printf "%X\n" $a
done