#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Can you generare Hex decimal numbes using perl code


    Hi there,

    I was is any way that we can generate Hexa decimal number in perl. My requirement is if 0001 is given and I need next 40 hexa decimal numbers starting from 0001 to 000f then next number would be 0010 to 001f then next number would be 0020 to 002f.... and so on till FFFF. Is there any code that generate numbers in following format.
    I have more question. Like can we look for all the missing number in a file. if you think that there is a file with number and i need to find all the missing numbers in that file.
    Please help me in coding . Thanks
  2. #2
  3. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    83
    Rep Power
    142
    Use sprintf:
    perl Code:
    #!/usr/bin/perl
     
    use strict;
    use warnings;
     
    for my $n ( 0 .. 0xffff ){
      my $hex = sprintf '%04x', $n;
      print "$hex\n";
    }


    See http://perldoc.perl.org/functions/sprintf.html

    Comments on this post

    • ishnid agrees
    Don't stop where the ink does.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    thanks


    hi there,
    I think i wrote the code to generate hexa decimal numbers.
    this is the code.
    Code:
    #!/usr/bin/perl -w
    use Bit::Vector;
    my $file;
    my @final_hex_values;
    print " Enter the palce where do you want to keep your file\n";
    chomp($file=<STDIN>);
    open(HEX_VAL,">$file") or die " Unable to open the file $file \n";
    print " Geneerating hexa decimal numbers \n";
    print " Enter the hexa decimal starting number \n";
    chomp (my $start_hex_no=<STDIN>);
    my $deci_val=hex($start_hex_no);
    print " The decimal value of $start_hex_no is  $deci_val \n";
    print " How many Hexa decimal numbers do you need starting from $start_hex_no\n";
    chomp(my $count=<STDIN>);
    for(my $i=0;$i<$count;$i++)
    {
      $vec = Bit::Vector->new_Dec(16, $deci_val);
        $hex = $vec->to_Hex();
    	print HEX_VAL " $hex\n";
    	$final_hex_values[$i]=$hex;
      $deci_val++;
    }
    
    foreach(@final_hex_values){
    print " $_\n";
    }
    I was wondering can you send a code to find missing hexa decimal numbers in a file. for example.

    0001
    0003
    0005
    .............
    in the above it is missing 0000,0002,0004....... I need to find out only missing hexa decimal number. i don't know what is end point.
    thanks in advance.
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,878
    Rep Power
    3891
    Originally Posted by jpkumar10
    Code:
    $vec = Bit::Vector->new_Dec(16, $deci_val);
        $hex = $vec->to_Hex();
    Why not stick to the builtin 'sprintf' function, as mentioned above, rather than introducing non-standard modules into your code?

    To compare the values from the file: depending on the size of the file, you could read it into a hash, with the hex values as the hash keys. Then when you generate your hex values, you can check the hash to see if was in the file.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0
    I need hex decimal from a specified number that I will give as an input. I need a specified number of hexa decimal number starting from a input number. It is working for me. .
  10. #6
  11. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,878
    Rep Power
    3891
    Originally Posted by jpkumar10
    I need hex decimal from a specified number that I will give as an input. I need a specified number of hexa decimal number starting from a input number. It is working for me. .
    I'm not claiming that your code doesn't work: just that using sprintf, as shawnhcorey suggested, is a better way of achieving the same thing.

IMN logo majestic logo threadwatch logo seochat tools logo