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
Call me generous today, but I threw this together quickly and tested it against the sample you provided. It should work for you: -
#!/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");
-
}
-
10 3613
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
Call me generous today, but I threw this together quickly and tested it against the sample you provided. It should work for you: -
#!/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");
-
}
-
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
Anytime! Glad it works for you.
Regards,
Jeff
This gives you the output you requested. It is necessary to convert your strings to hex first: -
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
-
Jeff,
When I run your code, I don't see 002B, 002C, etc., as in the original question.
Ah, that was my mistake. Good catch @toolic!
Regards,
Jeff
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
Can you please post your code so we may see it?
Thanks!
Jeff
Thanks Jeff.... Here is the what I used on windows OS.. -
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
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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);
}
|
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...
|
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 =>...
|
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.:...
|
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...
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - How do I convert a Number into a String with
exactly 2 decimal places?...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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: 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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |