Solved

Module to convert any multiple of bytes to any other multiple?

Posted on 2000-03-09
4
199 Views
Last Modified: 2012-05-04
Is there a module that can handle conversion from any multiple of byte to any other multiple?  For example

$GB = &convert(kB,GB,$num);

This sub would take the first arg as what $num IS (a # of kB) and transform to GB.

$kB = &convert(GB,kB,$num);

This would take $num understood to be in GB and convert to kB...

The idea is faily simple but the multiples get annoying to have to account for all cases...?
0
Comment
Question by:guadalupe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 3

Author Comment

by:guadalupe
ID: 2602416
Well I though about and came up with this code:

sub convert_k($$$)
{
      ($from,$to,$number) = @_;

      #Set up hash for math conversions between bytes - kB and MB GB
      $byteMath{"B"} = 1;

      $byteMath{"kB"} = 2;

      $byteMath{"MB"} = 3;

      $byteMath{"GB"} = 4;

      $byte_multiple = 1024;

      $power = $byteMath{$from} - $byteMath{$to};

      return $number * ($byte_multiple ** $power);
}

I had hoped to not have to right the code as no idea came to mind and then it hit me.  I would still be interested to know if a modual exists.  But the code above is good so take it if it interests you!
0
 
LVL 3

Author Comment

by:guadalupe
ID: 2602431
A stupid but good improvment:


#!/usr/local/bin/perl


print &convert_k($ARGV[0],$ARGV[1],$ARGV[2]);


sub convert_k($$$)
{
      ($from,$to,$number) = @_;

      $from      = uc($from);

      $to      = uc($to);

      #Set up hash for math conversions between bytes - kB and MB GB
      $byteMath{"B"} = 1;

      $byteMath{"KB"} = 2;

      $byteMath{"MB"} = 3;

      $byteMath{"GB"} = 4;

      $byte_multiple = 1024;

      $power = $byteMath{$from} - $byteMath{$to};

      return $number * ($byte_multiple ** $power);
}
0
 
LVL 3

Accepted Solution

by:
monas earned 100 total points
ID: 2602444
package KMGconv;
%mult =(
  B=>1,
  kB=>1024,
  MB=>1024*1024,
  GB=>1024*1024*1024,
  TB=>1024*1024*1024*1024,
  );

sub  convert{
  my($from, $to, $amount)=@_;
  return $amount*$mult{$from}/$mult{$to};
}

-------------------------
use KMGconv;

print KMGconv::convert(MB,kB, 12);

--------------------------

you can define more constant, or use 1000's if you prefer.

Good Look!
0
 
LVL 3

Author Comment

by:guadalupe
ID: 2602522
Thank you!  Very nice though I'm still curious if one already exists  just to see how "they" chose to do it...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question