Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating hex numbers of a certain range

Tags:

bash

shell

hex

I need a simple way to generate a .txt file with a list of semi-colon delimited hex numbers between (inclusive) a certain start and finish value.

For example:

If I enter 0 and FFF it would pad the output with zeroes to the largest number:

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

If I enter FFF and 1200 it would output those values...etc:

0FFF;1000;1001;1002;.....1200;

Any suggestions? I'm not a programmer so the best, simplest way to do this is way beyond me.

like image 862
Dave Avatar asked Apr 01 '11 18:04

Dave


People also ask

How many rows and columns are in a hex grid?

Hex number length. This example generates a hex grid with 16 rows and 16 columns. Each row has one hex number, and each column has a single hex digit. These options will be used automatically if you select this example. Number of hex results to generate. Hex number length.

How to generate random hexadecimal integers from a positive integer?

Given a positive integer N, the task is to generate N random hexadecimal integers. Approach: The given problem can be solved with the help of the rand () function which is used to generate random integers.

How do I get the hex code of an option?

Number of hex results to generate. Hex number length. You can pass options to this tool using their codes as query arguments and it will automatically compute output. To get the code of an option, just hover over its icon. Here's how to type it in your browser's address bar. Click to try! Quickly convert ASCII characters to hexadecimal numbers.

How to randomly select all the possible characters in hexadecimal?

A character array can be created which stores all the possible characters in the hexadecimal notation and randomly select the characters from the array. // This code is contributed by sanjoy_62.


1 Answers

I just like to use simple for loops. You can add as many nested loops as you like for each digit:

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

Output should look like this:

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;

That was a one-liner, but let me give it some structure:

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
like image 170
William Cates Avatar answered Sep 20 '22 19:09

William Cates