473,382 Members | 1,329 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,382 software developers and data experts.

expanding Hex number range not in continous formate

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
May 21 '15 #1
15 2110
computerfox
276 100+
Have you attempted to solve this problem on your own?
Can we see what you have so far?
May 21 '15 #2
computerfox
276 100+
Try this. It doesn't take data from a file, but it generates the range:
Expand|Select|Wrap|Line Numbers
  1. #!/bin/perl
  2. sub exp_hex(my $hex){
  3.  my $hex=shift;
  4.  my @comps;
  5.  @comps=split(":",$hex);
  6.  my @list;
  7.  if($#comps>0){
  8.   my $hex1=$comps[0];
  9.   my $hex2=$comps[1];
  10.   my $final="";
  11.   for($i=hex($hex1);$i<=hex($hex2);$i++){
  12.    $current=sprintf("%04X",$i)."\n";
  13.    push(@list,$current);
  14.   }
  15.   return @list;
  16.  }
  17.  else{
  18.   return "Not a valid range...";
  19.  }
  20.  return 0;
  21. }
  22. sub menu(){
  23.  print "perl exp_hex.pl \${hex_string}, where \${hex_string} is mandatory....";
  24.  print "\nThis script assumes that you put the correct data for the range...";
  25. }
  26. ##int main
  27. if($#ARGV>-1){
  28.  print &exp_hex($ARGV[0]);
  29. }
  30. else{
  31.  print "Please provide input...";
  32.  print "\n";
  33.  &menu();
  34. }
  35. print "\n";
  36.  
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
May 21 '15 #3
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.
May 21 '15 #4
computerfox
276 100+
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.
May 21 '15 #5
computerfox
276 100+
Here's another version for Ron's sake....

Expand|Select|Wrap|Line Numbers
  1. #!/bin/perl
  2. use strict;
  3.  
  4. sub exp_hex(my $hex){
  5.  my $hex=shift;
  6.  my @comps;
  7.  @comps=split(":",$hex);
  8.  my @list;
  9.  if($#comps>0){
  10.   my $hex1=$comps[0];
  11.   my $hex2=$comps[1];
  12.   my $final="";
  13.   for(my $i=hex($hex1);$i<=hex($hex2);$i++){
  14.    my $current=sprintf("%04X",$i)."\n";
  15.    push(@list,$current);
  16.   }
  17.   return @list;
  18.  }
  19.  else{
  20.   return "Not a valid range...";
  21.  }
  22.  return 0;
  23. }
  24. sub menu(){
  25.  print "perl exp_hex.pl \${hex_string}, where \${hex_string} is mandatory....";
  26.  print "\nThis script assumes that you put the correct data for the range...";
  27. }
  28. ##int main
  29. if($#ARGV>-1){
  30.  print &exp_hex($ARGV[0]);
  31. }
  32. else{
  33.  print "Please provide input...";
  34.  print "\n";
  35.  &menu();
  36. }
  37. print "\n";
  38.  
http://safe.abelgancsos.com/codepost...ect.php?id=387
May 21 '15 #6
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.

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. while (<DATA>) {
  7.     chomp;
  8.     my $i = 0;
  9.     my ($start, $end) = split /:/;
  10.     for (hex($start) .. hex($end)) {
  11.         my $mod = $i % 4;
  12.         printf("%X\n", $_) if $mod == 0;
  13.         $i++;
  14.     }
  15. }
  16.  
  17. __DATA__
  18. 0x0:0xF
May 21 '15 #7
computerfox
276 100+
Fixed that warning too.
Expand|Select|Wrap|Line Numbers
  1. #!/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. sub exp_hex(){
  6.  my $hex=shift;
  7.  my @comps;
  8.  @comps=split(":",$hex);
  9.  my @list;
  10.  if($#comps>0){
  11.   my $hex1=$comps[0];
  12.   my $hex2=$comps[1];
  13.   my $final="";
  14.   for(my $i=hex($hex1);$i<=hex($hex2);$i++){
  15.    my $current=sprintf("%04X",$i)."\n";
  16.    push(@list,$current);
  17.   }
  18.   return @list;
  19.  }
  20.  else{
  21.   return "Not a valid range...";
  22.  }
  23.  return 0;
  24. }
  25. sub menu(){
  26.  print "perl exp_hex.pl \${hex_string}, where \${hex_string} is mandatory....";
  27.  print "\nThis script assumes that you put the correct data for the range...";
  28. }
  29. ##int main
  30. if($#ARGV>-1){
  31.  print &exp_hex($ARGV[0]);
  32. }
  33. else{
  34.  print "Please provide input...";
  35.  print "\n";
  36.  &menu();
  37. }
  38. print "\n";
  39.  
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.
May 21 '15 #8
computerfox
276 100+
Oh and by the way... Ran the code that you posted:

Expand|Select|Wrap|Line Numbers
  1.  
  2. 4
  3. 8
  4. C
  5. Use of uninitialized value $start in hex at ron_test.pl line 10, <DATA> line 2.
  6. Use of uninitialized value $end in hex at ron_test.pl line 10, <DATA> line 2.
  7.  
  8.  
You might want to check the code prior to copy/paste.
May 21 '15 #9
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.
May 21 '15 #10
computerfox
276 100+
Anyway....

As for the OP, this is my version of the code that produces the same output without any error/warning.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. sub exp_hex(){
  6.  my $hex=shift;
  7.  my @comps;
  8.  @comps=split(":",$hex);
  9.  my @list;
  10.  if($#comps>0){
  11.   my $hex1=$comps[0];
  12.   my $hex2=$comps[1];
  13.   my $final="";
  14.   for(my $i=hex($hex1);$i<=hex($hex2);$i++){
  15.    my $current=sprintf("%1X",$i)."\n";
  16.    if($i%4==0){
  17.     push(@list,$current);
  18.    }
  19.   }
  20.   return @list;
  21.  }
  22.  else{
  23.   return "Not a valid range...";
  24.  }
  25.  return 0;
  26. }
  27. sub menu(){
  28.  print "perl exp_hex.pl \${hex_string}, where \${hex_string} is mandatory....";
  29.  print "\nThis script assumes that you put the correct data for the range...";
  30. }
  31. ##int main
  32. if($#ARGV>-1){
  33.  print &exp_hex($ARGV[0]);
  34. }
  35. else{
  36.  print "Please provide input...";
  37.  print "\n";
  38.  &menu();
  39. }
  40. print "\n";
  41.  
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.
May 21 '15 #11
RonB
589 Expert Mod 512MB
The warning you received was caused by you adding an additional line to the end of the DATA section.

Expand|Select|Wrap|Line Numbers
  1. D:\test>type Perl-1.pl
  2. #!/usr/bin/perl
  3.  
  4. use warnings;
  5. use strict;
  6.  
  7. while (<DATA>) {
  8.     chomp;
  9.     my $i = 0;
  10.     my ($start, $end) = split /:/;
  11.     for (hex($start) .. hex($end)) {
  12.         my $mod = $i % 4;
  13.         printf("%X\n", $_) if $mod == 0;
  14.         $i++;
  15.     }
  16. }
  17.  
  18. __DATA__
  19. 0x0:0xF
  20.  
D:\test>Perl-1.pl
0
4
8
C

D:\test>
May 21 '15 #12
computerfox
276 100+
Confirmed....
Expand|Select|Wrap|Line Numbers
  1.  
  2. 4
  3. 8
  4. C
  5.  
May 21 '15 #13
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.
May 21 '15 #14
computerfox
276 100+
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?
May 21 '15 #15
RonB
589 Expert Mod 512MB
It probably is, but I'm not sure.
May 21 '15 #16

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

Similar topics

0
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...
45
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
13
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...
4
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
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...
5
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...
11
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...
10
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
1
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...
0
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...
0
isladogs
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...
0
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,...
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.