473,595 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

expanding Hex number range not in continous formate

1 New Member
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 2137
computerfox
276 Contributor
Have you attempted to solve this problem on your own?
Can we see what you have so far?
May 21 '15 #2
computerfox
276 Contributor
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 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.
May 21 '15 #4
computerfox
276 Contributor
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.
May 21 '15 #5
computerfox
276 Contributor
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 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.

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 Contributor
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 Contributor
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 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.
May 21 '15 #10

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

Similar topics

0
1154
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
45
8547
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
61496
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)
4
1604
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
29518
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 ?
5
2850
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:
11
2620
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...
10
3633
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
3377
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
0
7957
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, 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...
1
8020
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,...
0
6675
agi2029
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...
0
5421
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();...
0
3875
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...
0
3915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
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
1
1491
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1226
bsmnconsultancy
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...

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.