Expand a Hex number range with PERL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fil66
    New Member
    • Mar 2010
    • 4

    Expand a Hex number range with PERL

    Hi There,

    Is it possible to expand Hex number range?

    Here is what I have in a text file...

    002A:002F
    03E0:03E0
    03E2:03E2
    07AF:07B1
    0212:0212
    0248:0248

    I would like to expand the range (when there is a range) and have all including the leading 0's.

    Here is the output I am looking for...

    002A
    002B
    002C
    002D
    002E
    002F
    03E0
    03E2
    07AF
    07B0
    07B1
    0212
    0248

    All the information I found was to convert a Hex number.... any help would be appreciated.

    Thanks, Fil
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    why not split each range using the ":" as the delimiter and put each element into an array. Then, sort the array and print it. Just an idea. Try to do it, then if you get stuck, paste your code here.

    Regards,

    Jeff

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Call me generous today, but I threw this together quickly and tested it against the sample you provided. It should work for you:

      Code:
      #!/usr/bin/perl
      
      use warnings;
      use strict;
      
      
      open(FILE, "<data.txt") or die "Unable to open data file:  $!";
      
      my @hexarray;
      my $line;
      my $var1;
      my $var2;
      
      while(<FILE>){
          $line = $_;
          
          ($var1, $var2) = split(/\:/, $line);
         
          chomp($var2);
          push(@hexarray, $var1);
          push(@hexarray, $var2);
      }
      
      foreach my $hexvalue(sort(@hexarray)){
          print("$hexvalue\n");
      }

      Comment

      • fil66
        New Member
        • Mar 2010
        • 4

        #4
        WOW... that is fantastic.. Thank you numberwhun..

        It does exactly what I need plus keeps the number format (all leading zeros)...
        Appreciate your time..JFC

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Anytime! Glad it works for you.

          Regards,

          Jeff

          Comment

          • toolic
            Recognized Expert New Member
            • Sep 2009
            • 70

            #6
            This gives you the output you requested. It is necessary to convert your strings to hex first:
            Code:
            use strict;
            use warnings;
            
            while (<DATA>) {
                chomp;
                my ($n1, $n2) = split /:/;
                for (hex($n1) .. hex($n2)) {
                    printf "%04X\n", $_;
                }
            }
            __DATA__
            002A:002F
            03E0:03E0
            03E2:03E2
            07AF:07B1
            0212:0212
            0248:0248

            Comment

            • toolic
              Recognized Expert New Member
              • Sep 2009
              • 70

              #7
              Jeff,

              When I run your code, I don't see 002B, 002C, etc., as in the original question.

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                Ah, that was my mistake. Good catch @toolic!

                Regards,

                Jeff

                Comment

                • fil66
                  New Member
                  • Mar 2010
                  • 4

                  #9
                  Thanks toolic for catching it... very important to have all the devices in between.
                  I tried your Perl scritp and keep getting an error ... I am not sure what I am doing wrong.

                  Here is the error
                  C:\>\expandrang e.pl
                  Illegal hexadecimal digit 'T' ignored at \expandrange.pl line 7.
                  Use of uninitialized value $n2 in hex at \expandrange.pl line 7.

                  same data with a file name DATA.txt

                  Comment

                  • numberwhun
                    Recognized Expert Moderator Specialist
                    • May 2007
                    • 3467

                    #10
                    Can you please post your code so we may see it?

                    Thanks!

                    Jeff

                    Comment

                    • fil66
                      New Member
                      • Mar 2010
                      • 4

                      #11
                      Thanks Jeff.... Here is the what I used on windows OS..
                      Code:
                      use strict; 
                      use warnings; 
                        
                      while (<DATA.txt>) { 
                          chomp; 
                          my ($n1, $n2) = split /:/; 
                          for (hex($n1) .. hex($n2)) { 
                              printf "%04X\n", $_; 
                          } 
                      } 
                      
                      
                      
                      ___ DATA.txt ____
                      
                      002A:002F
                      03E0:03E0
                      03E2:03E2
                      07AF:07B1
                      0212:0212
                      0248:0248
                      Last edited by numberwhun; Mar 30 '10, 02:49 AM. Reason: Please use CODE TAGS!!!

                      Comment

                      Working...