Generate a range of hexadecimal numbers

I need a simple way to generate a .txt file with a list of hexadecimal numbers with comma delimiters between (inclusive) a specific start and end value.

For instance:

If I go into 0and FFF, it will set the output with zeros to the largest number:

000; 001; 002; 003; 004; 005; 006; 007; 008; 009; 00A; 00V; 00C; 00D; 00E; 00F; 010; 011; .... FFF;

If I enter FFFand 1200, it will output these values ​​... etc .:

0FFF; 1000; 1001; 1 002; ..... 1200;

Any suggestions? I am not a programmer, so the best, easiest way to do this is far from me.

+5
source share
7 answers

script, ( POSIX ), ( nawk, bc ..) .

#!/bin/sh
#
# hexcount -  POSIX Shell script to count from $1 to $2 in hex,
#             separated by ";" and with the precision set to the
#             maximum digits of $1 and $2.
# Usage:      hexcount lo hi
# Example:    hexcount FFF 1200

from=$1 to=$2
if test "${#from}" -gt "${#to}"; then
    format="%0${#from}X;"
else
    format="%0${#to}X;"
fi
from=$(printf '%d' "0x$from") to=$(printf '%d' "0x$to")
while test "$from" -le "$to"; do
    printf "$format" "$from"
    from=$((from+1))
done
printf '\n'

printf?

+9

, KSH OpenBSD ( Bourne, bash).

#!/bin/sh

START=$1
END=$2

function h2d {
    echo "ibase=16; $@"|bc
}

function d2h {
    echo "obase=16; $@"|bc
}

# convert to decimal
START_DEC=`h2d ${START}`
END_DEC=`h2d ${END}`

# find length of last hex number
LENGTH=`expr ${#END} - 1`

for i in `nawk "BEGIN{ for(i=${START_DEC};i<=${END_DEC};i++) print i}"`
do
    # convert output to hex
    OUTPUT=`d2h ${i}`

    # calculate output length
    OUTPUT_LENGTH=`expr ${#OUTPUT}`

    # calculate required padding
    PAD_LENGTH=`expr ${LENGTH} - ${OUTPUT_LENGTH}`

    # output padding
    for j in `nawk "BEGIN{ for(j=0;j<=${PAD_LENGTH};j++) print j}"`
    do
        echo -n 0
    done

    # output number
    echo -n ${OUTPUT}\;
done

# for the newline
echo

:

    $ ./hex-range.sh 91 FF
91;92;93;94;95;96;97;98;99;9A;9B;9C;9D;9E;9F;A0;A1;A2;A3;A4;A5;A6;A7;A8;A9;AA;AB;AC;AD;AE;AF;B0;B1;B2;B3;B4;B5;B6;B7;B8;B9;BA;BB;BC;BD;BE;BF;C0;C1;C2;C3;C4;C5;C6;C7;C8;C9;CA;CB;CC;CD;CE;CF;D0;D1;D2;D3;D4;D5;D6;D7;D8;D9;DA;DB;DC;DD;DE;DF;E0;E1;E2;E3;E4;E5;E6;E7;E8;E9;EA;EB;EC;ED;EE;EF;F0;F1;F2;F3;F4;F5;F6;F7;F8;F9;FA;FB;FC;FD;FE;FF;

, :

 $./hex-range.sh 91 FF > output.txt
+2

(- seq, bc) - cygwin linux:

seq 0 255 | while read n; do printf "%04X;" $n; done

, hex, , , "04" printf

+2

script , , :

#!/bin/sh
#Script to test for loop
i=10 #put start num in here
j=20 #put end num in here
for ((  a=i ;  a <= j;  a++  ))
do
printf '&#x%05x;' "$a" >> output.txt
done
0

, . , 0 255. :

for n in $(seq 0 255); do printf "\x$(echo "obase=16; $n" | bc)" >> seq.bin; done

, , , .

, .

0

. , :

for i in {0..9} {a..f}; do for x in {0..9} {a..f}; do for y in {0..9} {a..f}; do printf "$i$x$y;"; done; done; done

:

000;001;002;003;004;005;006;007;008;009;00a;00b;00c;00d;00e;00f;010;011;012;013;014;015;016;017;018...ff8;ff9;ffa;ffb;ffc;ffd;ffe;fff;

, :

for i in {0..9} {a..f}
do
   for x in {0..9} {a..f}
   do
      for y in {0..9} {a..f}
      do
         printf "$i$x$y;"
      done
   done
done
0

, ( ), -:

bc <<< "obase=16; ibase=16; for (i = $low; i <= $high ; i++) i"

here ( bc ), bc . ibase=16 ( obase - , - ).

, , seq, GNU , :

, printf :

$ printf '%x\n' $(seq 1048575 1024 1050623)

fffff

1003ff

1007ff

0

All Articles