expanding Hex number range not in continous formate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apurvabhatt
    New Member
    • May 2015
    • 1

    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
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    Have you attempted to solve this problem on your own?
    Can we see what you have so far?

    Comment

    • computerfox
      Contributor
      • Mar 2010
      • 276

      #3
      Try this. It doesn't take data from a file, but it generates the range:
      Code:
      #!/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";


      Just an FYI, I found this from 5 years ago...

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        @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.

        Comment

        • computerfox
          Contributor
          • Mar 2010
          • 276

          #5
          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.
          Last edited by computerfox; May 21 '15, 06:31 PM. Reason: Grammar;Additional justification

          Comment

          • computerfox
            Contributor
            • Mar 2010
            • 276

            #6
            Here's another version for Ron's sake....

            Code:
            #!/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";

            Comment

            • RonB
              Recognized Expert Contributor
              • Jun 2009
              • 589

              #7
              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.

              Code:
              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.

              Code:
              #!/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

              Comment

              • computerfox
                Contributor
                • Mar 2010
                • 276

                #8
                Fixed that warning too.
                Code:
                #!/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.
                Last edited by computerfox; May 21 '15, 07:02 PM. Reason: Spelling

                Comment

                • computerfox
                  Contributor
                  • Mar 2010
                  • 276

                  #9
                  Oh and by the way... Ran the code that you posted:

                  Code:
                  0
                  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.
                  0
                  You might want to check the code prior to copy/paste.

                  Comment

                  • RonB
                    Recognized Expert Contributor
                    • Jun 2009
                    • 589

                    #10
                    computerfox,

                    I apologize for offending you. Doing so was not my intention.

                    My intention was to point out better coding practices.

                    Comment

                    • computerfox
                      Contributor
                      • Mar 2010
                      • 276

                      #11
                      Anyway....

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

                      Code:
                      #!/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";


                      For any alternatives, please provide another solution that produces the same output without any errors/warnings.
                      Last edited by computerfox; May 21 '15, 07:21 PM. Reason: Citation

                      Comment

                      • RonB
                        Recognized Expert Contributor
                        • Jun 2009
                        • 589

                        #12
                        The warning you received was caused by you adding an additional line to the end of the DATA section.

                        Code:
                        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>
                        Last edited by RonB; May 21 '15, 07:53 PM.

                        Comment

                        • computerfox
                          Contributor
                          • Mar 2010
                          • 276

                          #13
                          Confirmed....
                          Code:
                          0
                          4
                          8
                          C

                          Comment

                          • RonB
                            Recognized Expert Contributor
                            • Jun 2009
                            • 589

                            #14
                            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.

                            Comment

                            • computerfox
                              Contributor
                              • Mar 2010
                              • 276

                              #15
                              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?

                              Comment

                              Working...