469,328 Members | 1,260 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Expand a Hex number range with PERL

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.  

10 3348
numberwhun
3,503 Expert Mod 2GB
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
3,503 Expert Mod 2GB
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
fil66
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
3,503 Expert Mod 2GB
Anytime! Glad it works for you.

Regards,

Jeff
Mar 25 '10 #5
toolic
70 Expert
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
toolic
70 Expert
Jeff,

When I run your code, I don't see 002B, 002C, etc., as in the original question.
Mar 26 '10 #7
numberwhun
3,503 Expert Mod 2GB
Ah, that was my mistake. Good catch @toolic!

Regards,

Jeff
Mar 26 '10 #8
fil66
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
3,503 Expert Mod 2GB
Can you please post your code so we may see it?

Thanks!

Jeff
Mar 29 '10 #10
fil66
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.

Similar topics

7 posts views Thread by Xah Lee | last post: by
15 posts views Thread by Xah Lee | last post: by
1 post views Thread by Paul Porcelli | last post: by
1 post views Thread by Thomas | last post: by
30 posts views Thread by bdsatish | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.