By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,708 Members | 2,080 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,708 IT Pros & Developers. It's quick & easy.

Expand a Hex number range with PERL

P: 4
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
Mar 24 '10 #1

✓ answered by numberwhun

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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6.  
  7. open(FILE, "<data.txt") or die "Unable to open data file:  $!";
  8.  
  9. my @hexarray;
  10. my $line;
  11. my $var1;
  12. my $var2;
  13.  
  14. while(<FILE>){
  15.     $line = $_;
  16.  
  17.     ($var1, $var2) = split(/\:/, $line);
  18.  
  19.     chomp($var2);
  20.     push(@hexarray, $var1);
  21.     push(@hexarray, $var2);
  22. }
  23.  
  24. foreach my $hexvalue(sort(@hexarray)){
  25.     print("$hexvalue\n");
  26. }
  27.  

Share this Question
Share on Google+
10 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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
Mar 24 '10 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
Call me generous today, but I threw this together quickly and tested it against the sample you provided. It should work for you:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6.  
  7. open(FILE, "<data.txt") or die "Unable to open data file:  $!";
  8.  
  9. my @hexarray;
  10. my $line;
  11. my $var1;
  12. my $var2;
  13.  
  14. while(<FILE>){
  15.     $line = $_;
  16.  
  17.     ($var1, $var2) = split(/\:/, $line);
  18.  
  19.     chomp($var2);
  20.     push(@hexarray, $var1);
  21.     push(@hexarray, $var2);
  22. }
  23.  
  24. foreach my $hexvalue(sort(@hexarray)){
  25.     print("$hexvalue\n");
  26. }
  27.  
Mar 24 '10 #3

P: 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
Mar 24 '10 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
Anytime! Glad it works for you.

Regards,

Jeff
Mar 25 '10 #5

Expert
P: 70
This gives you the output you requested. It is necessary to convert your strings to hex first:
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. while (<DATA>) {
  5.     chomp;
  6.     my ($n1, $n2) = split /:/;
  7.     for (hex($n1) .. hex($n2)) {
  8.         printf "%04X\n", $_;
  9.     }
  10. }
  11. __DATA__
  12. 002A:002F
  13. 03E0:03E0
  14. 03E2:03E2
  15. 07AF:07B1
  16. 0212:0212
  17. 0248:0248
  18.  
Mar 26 '10 #6

Expert
P: 70
Jeff,

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

numberwhun
Expert Mod 2.5K+
P: 3,503
Ah, that was my mistake. Good catch @toolic!

Regards,

Jeff
Mar 26 '10 #8

P: 4
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:\>\expandrange.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
Mar 29 '10 #9

numberwhun
Expert Mod 2.5K+
P: 3,503
Can you please post your code so we may see it?

Thanks!

Jeff
Mar 29 '10 #10

P: 4
Thanks Jeff.... Here is the what I used on windows OS..
Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings; 
  3.  
  4. while (<DATA.txt>) { 
  5.     chomp; 
  6.     my ($n1, $n2) = split /:/; 
  7.     for (hex($n1) .. hex($n2)) { 
  8.         printf "%04X\n", $_; 
  9.     } 
  10.  
  11.  
  12.  
  13. ___ DATA.txt ____
  14.  
  15. 002A:002F
  16. 03E0:03E0
  17. 03E2:03E2
  18. 07AF:07B1
  19. 0212:0212
  20. 0248:0248
  21.  
Mar 29 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.