473,388 Members | 884 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,388 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 3610
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.