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 2137
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
Recognized Expert Moderator Contributor
@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;hum bly) 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
Recognized Expert Moderator Contributor
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
Recognized Expert Moderator Contributor
computerfox,
I apologize for offending you. Doing so was not my intention.
My intention was to point out better coding practices.
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
I wish to add the following 9 records to the table.
AA123453
|
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 random numbers between 100 and 200)
|
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 expected
my_round(2.5) = 2 # Not 3, which is an odd num
I'm interested in rounding numbers of the form "x.5" depending upon
whether x is odd or even. Any idea about how to implement it ?
| |
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 Access and VB with no results. I don’t even know if is possible.
Here is the Question:
|
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 form which works but how do I do it for a range of serial numbers and include some values that don't change?
Here is an example of what they want to do:
Serial number range: abc123 - abc456
Part Number: A234J1
PO Number: 5008
DateEntered: 3/10/2010...
|
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 convert all numbers entered into a string
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |