Hi There,
I want to expand Hex number range but not the continuous manner
Here is what I have input text file...
0x0:0xF
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...
0
4
8
C
All the information I found was to generate continuous Hex number.... any help would be appreciated.
Thanks & Regards
Apurva
15 2110
Have you attempted to solve this problem on your own?
Can we see what you have so far?
Try this. It doesn't take data from a file, but it generates the range: -
#!/bin/perl
-
sub exp_hex(my $hex){
-
my $hex=shift;
-
my @comps;
-
@comps=split(":",$hex);
-
my @list;
-
if($#comps>0){
-
my $hex1=$comps[0];
-
my $hex2=$comps[1];
-
my $final="";
-
for($i=hex($hex1);$i<=hex($hex2);$i++){
-
$current=sprintf("%04X",$i)."\n";
-
push(@list,$current);
-
}
-
return @list;
-
}
-
else{
-
return "Not a valid range...";
-
}
-
return 0;
-
}
-
sub menu(){
-
print "perl exp_hex.pl \${hex_string}, where \${hex_string} is mandatory....";
-
print "\nThis script assumes that you put the correct data for the range...";
-
}
-
##int main
-
if($#ARGV>-1){
-
print &exp_hex($ARGV[0]);
-
}
-
else{
-
print "Please provide input...";
-
print "\n";
-
&menu();
-
}
-
print "\n";
-
http://safe.abelgancsos.com/codepost...ect.php?id=385
Just an FYI, I found this from 5 years ago... http://bytes.com/topic/perl/answers/...ber-range-perl RonB 589
Expert Mod 512MB
@computerfox,
I don't wish to offend you, but that is very poor quality code.
You should not use prototypes unless you know about and want/need their side effects. And, declaring a lexical var in place of a prototype definition is invalid syntax.
You're failing to use the strict and warnings pragmas, which should be in every script. If you had used them, the script would not even compile.
The C style for loop is messy and not as efficient as perl's normal for loop construct.
There are about a half dozen other issues I can point out, but I don't want to go overboard.
Hi Ron, please, why stop now...
Oh, and by the way, at least I provided a solution for the OP instead of just criticizing a user's comment. If you really cared to help the OP, you would have answered it a long time ago.
In addition, even though I shouldn't have to justify my code to a person who is neither my lead engineer nor a user who cares, I built the script the way I did for portability so that if the OP wishes to port the code over to another script for any other use, he can do that with ease. Having prototypes makes it easier to do that. The only issue I see, as also you are aware, would be that it might slow the script down. I slightly risk the speed for a script that isn't robust for it's portability. Another issue you might have with my code is the location of the braces. Yes, I don't use a whole line just for a brace, which looks cleaner to me.
No offense, but your way of coding isn't the only way to code and I (thankfully;humbly) have had compliments on my code, whereas I haven't seen a line of code from you.
Here's another version for Ron's sake.... -
#!/bin/perl
-
use strict;
-
-
sub exp_hex(my $hex){
-
my $hex=shift;
-
my @comps;
-
@comps=split(":",$hex);
-
my @list;
-
if($#comps>0){
-
my $hex1=$comps[0];
-
my $hex2=$comps[1];
-
my $final="";
-
for(my $i=hex($hex1);$i<=hex($hex2);$i++){
-
my $current=sprintf("%04X",$i)."\n";
-
push(@list,$current);
-
}
-
return @list;
-
}
-
else{
-
return "Not a valid range...";
-
}
-
return 0;
-
}
-
sub menu(){
-
print "perl exp_hex.pl \${hex_string}, where \${hex_string} is mandatory....";
-
print "\nThis script assumes that you put the correct data for the range...";
-
}
-
##int main
-
if($#ARGV>-1){
-
print &exp_hex($ARGV[0]);
-
}
-
else{
-
print "Please provide input...";
-
print "\n";
-
&menu();
-
}
-
print "\n";
-
http://safe.abelgancsos.com/codepost...ect.php?id=387 RonB 589
Expert Mod 512MB
You fixed 1 problem but there lots of others. Your code produces the following warning, which you ignore because when you told perl you didn't want to know about the warnings when you left out the warnings pragma. - Illegal character in prototype for main::exp_hex : my$hex at computerfox.pl line 4.
Also, your code still doesn't do what the OP wants. - #!/usr/bin/perl
-
-
use warnings;
-
use strict;
-
-
while (<DATA>) {
-
chomp;
-
my $i = 0;
-
my ($start, $end) = split /:/;
-
for (hex($start) .. hex($end)) {
-
my $mod = $i % 4;
-
printf("%X\n", $_) if $mod == 0;
-
$i++;
-
}
-
}
-
-
__DATA__
-
0x0:0xF
Fixed that warning too. -
#!/bin/perl
-
use strict;
-
use warnings;
-
-
sub exp_hex(){
-
my $hex=shift;
-
my @comps;
-
@comps=split(":",$hex);
-
my @list;
-
if($#comps>0){
-
my $hex1=$comps[0];
-
my $hex2=$comps[1];
-
my $final="";
-
for(my $i=hex($hex1);$i<=hex($hex2);$i++){
-
my $current=sprintf("%04X",$i)."\n";
-
push(@list,$current);
-
}
-
return @list;
-
}
-
else{
-
return "Not a valid range...";
-
}
-
return 0;
-
}
-
sub menu(){
-
print "perl exp_hex.pl \${hex_string}, where \${hex_string} is mandatory....";
-
print "\nThis script assumes that you put the correct data for the range...";
-
}
-
##int main
-
if($#ARGV>-1){
-
print &exp_hex($ARGV[0]);
-
}
-
else{
-
print "Please provide input...";
-
print "\n";
-
&menu();
-
}
-
print "\n";
-
Ran, with no further issues and yes I'm aware that the code does something slightly different (doesn't read the data from a file) than the OP, which is why I added the disclaimer. "Your" code doesn't do that either and I believe that's the same code as in the other thread. I'm beginning to think that you're just a troll.... You do know that as a "mod" you're suppose to be monitoring the threads if the rules are being followed, not acting like your coding methods are the only solutions and harassing other users. That's also another reason why I wrote my code the way I did so I give him another solution, not just copy/paste code from others.
Oh and by the way... Ran the code that you posted: -
-
4
-
8
-
C
-
Use of uninitialized value $start in hex at ron_test.pl line 10, <DATA> line 2.
-
Use of uninitialized value $end in hex at ron_test.pl line 10, <DATA> line 2.
-
-
You might want to check the code prior to copy/paste.
RonB 589
Expert Mod 512MB
computerfox,
I apologize for offending you. Doing so was not my intention.
My intention was to point out better coding practices.
Anyway....
As for the OP, this is my version of the code that produces the same output without any error/warning. -
#!/bin/perl
-
use strict;
-
use warnings;
-
-
sub exp_hex(){
-
my $hex=shift;
-
my @comps;
-
@comps=split(":",$hex);
-
my @list;
-
if($#comps>0){
-
my $hex1=$comps[0];
-
my $hex2=$comps[1];
-
my $final="";
-
for(my $i=hex($hex1);$i<=hex($hex2);$i++){
-
my $current=sprintf("%1X",$i)."\n";
-
if($i%4==0){
-
push(@list,$current);
-
}
-
}
-
return @list;
-
}
-
else{
-
return "Not a valid range...";
-
}
-
return 0;
-
}
-
sub menu(){
-
print "perl exp_hex.pl \${hex_string}, where \${hex_string} is mandatory....";
-
print "\nThis script assumes that you put the correct data for the range...";
-
}
-
##int main
-
if($#ARGV>-1){
-
print &exp_hex($ARGV[0]);
-
}
-
else{
-
print "Please provide input...";
-
print "\n";
-
&menu();
-
}
-
print "\n";
-
http://safe.abelgancsos.com/codepost...ect.php?id=388
For any alternatives, please provide another solution that produces the same output without any errors/warnings.
RonB 589
Expert Mod 512MB
The warning you received was caused by you adding an additional line to the end of the DATA section. - D:\test>type Perl-1.pl
-
#!/usr/bin/perl
-
-
use warnings;
-
use strict;
-
-
while (<DATA>) {
-
chomp;
-
my $i = 0;
-
my ($start, $end) = split /:/;
-
for (hex($start) .. hex($end)) {
-
my $mod = $i % 4;
-
printf("%X\n", $_) if $mod == 0;
-
$i++;
-
}
-
}
-
-
__DATA__
-
0x0:0xF
-
D:\test>Perl-1.pl
0
4
8
C
D:\test>
RonB 589
Expert Mod 512MB
Yes, that is the output the OP wanted. The only issue is for some reason the forum software is not showing the 0 on the first line of output. If you click on the edit button, you will see it just as you can see it on your system when you ran the script.
That kinda made me snicker....
I noticed that too. The parser for the forum software must have an issue with single 0's in code tags. Do we know if the forum is based around phpBB?
RonB 589
Expert Mod 512MB
It probably is, but I'm not sure.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Fiaz Idris |
last post by:
I am an access newbie and I have the following question.
Say, There is a text box in a form for input a number range and
the user inputs the following.
TextBox: AA123453,57-59,500,505-507,556...
|
by: Summercoolness |
last post by:
it seems that range() can be really slow:
the following program will run, and the last line shows how long it ran
for:
import time
startTime = time.time()
a = 1.0
|
by: prileep |
last post by:
if i use java.util.Random i can create random numbers.
But how do i can define a Random object that its methods like
nextInt() should return between two specified numbers(Eg: i want to create...
|
by: nirvana |
last post by:
I need to count the number of continous character occurances(more than
1) in a file, and replace it with a compressed version, like below
XYZAAAAAAAADEFAAcdAA --XYZ8ADEF2Acd2A
Thanks
Sumod
|
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: dio66 |
last post by:
Hi,
I’m basically a Newbie in this posting of new threads and in the MS Access as well.
I spend a lot of hours trying to get this answer out of the Internet and 6 different books of MS...
|
by: technet |
last post by:
I am using Access 2007. I created a serial number database that imports excel into a table. I have now been asked if they can import a range of numbers directly into access. I have a single entry...
|
by: fil66 |
last post by:
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
|
by: gem46 |
last post by:
how to find numbers entered in textbox that have a dash showing a range of numbers. UI enters numbers that can be anything. ie 1,2,5-10,55....etc.
need to find the numbers between 5 and 10 and...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |