473,433 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,433 software developers and data experts.

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 3616
numberwhun
3,509 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,509 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,509 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,509 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,509 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

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

Similar topics

0
by: Marc | last post by:
Hi all, Perplexed by this. I thought the sticky option was supposed to accomplish this task, but it doesn't seem to be working. Here is what I'm trying to accomplish. Window...
7
by: Xah Lee | last post by:
Today we'll be writing a function called Range. The Perl documentation is as follows. Perl & Python & Java Solutions will be posted in 48 hours. This is Perl-Python a-day. See...
15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
6
by: john brown | last post by:
I'm new to perl programming and would like some help if at all possible. I'm trying to make a simple script where I can input a filename. To be exact something like "/home/song1.mp3". I want to...
1
by: Paul Porcelli | last post by:
I have the following code(excerpt) which grabs some lines from a syslog file and adds any found in the range to an array. @lines=();@vvlines=(); $t = new Net::Telnet (Timeout => 30, Prompt =>...
1
by: Thomas | last post by:
Hi dudes, I have a range object for text (not the one in the IE, the one for Mozilla). Now I have a function to reduce/move the range in the text to the LEFT, e.g.:...
0
by: sysmanint1 | last post by:
I am a total neophyte at Visual Basic but found the following post and reply from Clint concerning a dynamic range. Also, have never "posted" to a discussion I have made a macro that works on...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
4
by: Aaryan123 | last post by:
Function to check a number between a range in perl ========================================= Back ground: This situation has been fixed in Perl5.005. Use of .. in a for loop will iterate over...
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.