MayaChemTools

   1 package ConversionsUtil;
   2 #
   3 # $RCSfile: ConversionsUtil.pm,v $
   4 # $Date: 2008/04/19 16:11:00 $
   5 # $Revision: 1.10 $
   6 #
   7 # Author: Manish Sud <msud@san.rr.com>
   8 #
   9 # Copyright (C) 2004-2008 Manish Sud. All rights reserved.
  10 #
  11 # This file is part of MayaChemTools.
  12 #
  13 # MayaChemTools is free software; you can redistribute it and/or modify it under
  14 # the terms of the GNU Lesser General Public License as published by the Free
  15 # Software Foundation; either version 3 of the License, or (at your option) any
  16 # later version.
  17 #
  18 # MayaChemTools is distributed in the hope that it will be useful, but without
  19 # any warranty; without even the implied warranty of merchantability of fitness
  20 # for a particular purpose.  See the GNU Lesser General Public License for more
  21 # details.
  22 #
  23 # You should have received a copy of the GNU Lesser General Public License
  24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
  25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
  26 # Boston, MA, 02111-1307, USA.
  27 #
  28 use 5.006;
  29 use strict;
  30 use Exporter;
  31 use Constants;
  32 
  33 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  34 
  35 $VERSION = '1.00';
  36 @ISA = qw(Exporter);
  37 
  38 # Groups of conversion functions...
  39 my(@MathConversions) = qw(DegreesToRadians RadiansToDegrees);
  40 my(@NumericBaseConversions) = qw(BinaryToDecimal DecimalToBinary HexadecimalToDecimal DecimalToHexadecimal OctalToDecimal DecimalToOctal BinaryToHexadecimal HexadecimalToBinary HexadecimalToOctal OctalToHexadecimal StringToBinary StringToHexadecimal);
  41 
  42 # Export all conversion functions...
  43 @EXPORT = (@MathConversions, @NumericBaseConversions);
  44 @EXPORT_OK = qw();
  45 
  46 %EXPORT_TAGS = (
  47 		math => [@MathConversions],
  48 		all  => [@EXPORT, @EXPORT_OK]
  49 	       );
  50 
  51 
  52 # Degrees to radians...
  53 sub DegreesToRadians ($;$) {
  54   my($Degrees, $IgnoreWrap) = @_;
  55   my($Radians, $WrapValue);
  56 
  57   $WrapValue = (defined($IgnoreWrap) && $IgnoreWrap) ? 0 : 1;
  58   if ($Degrees > 360 && $WrapValue) {
  59     $Degrees = $Degrees % 360;
  60   }
  61   $Radians = ($Degrees * TwoPi) / 360;
  62 
  63   return $Radians;
  64 }
  65 
  66 # Radians to degrees...
  67 sub RadiansToDegrees ($;$) {
  68   my($Radians, $IgnoreWrap) = @_;
  69   my($Degrees, $WrapValue);
  70 
  71   $WrapValue = (defined($IgnoreWrap) && $IgnoreWrap) ? 0 : 1;
  72   $Degrees = ($Radians * 360) / TwoPi;
  73   if ($Degrees > 360 && $WrapValue) {
  74     $Degrees = $Degrees % 360;
  75   }
  76 
  77   return $Degrees;
  78 }
  79 
  80 # Convert a binary string to a decimal number...
  81 sub BinaryToDecimal ($) {
  82   my($Value) = @_;
  83 
  84   if ($Value !~ /^0b/) {
  85     $Value = "0b${Value}";
  86   }
  87   return _ConvertToDecimal($Value);
  88 }
  89 
  90 # Convert a decimal number into a binary string...
  91 sub DecimalToBinary ($) {
  92   my($Value) = @_;
  93 
  94   return sprintf("%b", $Value);
  95 }
  96 
  97 # Convert a hexadecimal string to a decimal number...
  98 sub HexadecimalToDecimal ($) {
  99   my($Value) = @_;
 100 
 101   if ($Value !~ /^0x/) {
 102     $Value = "0x${Value}";
 103   }
 104   return _ConvertToDecimal($Value);
 105 }
 106 
 107 # Convert a decimal number into a hexadecimal string...
 108 sub DecimalToHexadecimal ($) {
 109   my($Value) = @_;
 110 
 111   return sprintf("%x", $Value);
 112 }
 113 
 114 # Convert an octal string to a decimal number...
 115 sub OctalToDecimal ($) {
 116   my($Value) = @_;
 117 
 118   if ($Value !~ /^0/) {
 119     $Value = "0${Value}";
 120   }
 121   return _ConvertToDecimal($Value);
 122 }
 123 
 124 # Convert a decimal number into an octal string...
 125 sub DecimalToOctal ($) {
 126   my($Value) = @_;
 127 
 128   return sprintf("%o", $Value);
 129 }
 130 
 131 # Convert string into a binary string. Going from left to right, two ways of arranging bits
 132 # inside each byte are available: Most Significat Bits (MSB) first or Least Significat Bits (LSB)
 133 # first. Default is MSB corresponding to  descending bits order (PerlSpeak) inside each
 134 # each packed byte (Most singificat bits first).
 135 #
 136 sub StringToBinary ($;$) {
 137   my($Value, $UseReverseBitOrder) = @_;
 138   my($BinTemplate);
 139 
 140   $BinTemplate = (defined($UseReverseBitOrder) && $UseReverseBitOrder) ? 'b*' : 'B*';
 141   return unpack($BinTemplate, $Value);
 142 }
 143 
 144 # Convert string into a hexadecimal string. Two ways of arranging nybbles (pair of 4 bits in each
 145 # byte) are available: high nybbles first or low nybbles first. Default is MSB corresponding to high
 146 # nybbles (PerlSpeak) first. Low and high nybbles correspond to pair of a low and high four bits in a byte.
 147 #
 148 sub StringToHexadecimal ($;$) {
 149   my($Value, $UseReverseBitOrder) = @_;
 150   my($HexTemplate);
 151 
 152   $HexTemplate = (defined($UseReverseBitOrder) && $UseReverseBitOrder) ? 'h*' : 'H*';
 153   return unpack($HexTemplate, $Value);
 154 }
 155 
 156 # Convert a binary string into a hexadecimal string...
 157 #
 158 sub BinaryToHexadecimal ($;$) {
 159   my($Value, $UseReverseBitOrder) = @_;
 160   my($BinTemplate, $HexTemplate);
 161 
 162   $BinTemplate = (defined($UseReverseBitOrder) && $UseReverseBitOrder) ? 'b*' : 'B*';
 163   $HexTemplate = (defined($UseReverseBitOrder) && $UseReverseBitOrder) ? 'h*' : 'H*';
 164 
 165   return unpack($HexTemplate, pack($BinTemplate, $Value));
 166 }
 167 
 168 # Convert a hexadecimal string into a binary string...
 169 #
 170 sub HexadecimalToBinary ($;$) {
 171   my($Value, $UseReverseBitOrder) = @_;
 172   my($BinTemplate, $HexTemplate);
 173 
 174   $BinTemplate = (defined($UseReverseBitOrder) && $UseReverseBitOrder) ? 'b*' : 'B*';
 175   $HexTemplate = (defined($UseReverseBitOrder) && $UseReverseBitOrder) ? 'h*' : 'H*';
 176 
 177   return unpack($BinTemplate, pack($HexTemplate, $Value));
 178 }
 179 
 180 # Convert a hexadecimal string into a octal string...
 181 #
 182 sub HexadecimalToOctal {
 183   my($Hexadecimal) = @_;
 184 
 185   return DecimalToOctal(HexadecimalToDecimal($Hexadecimal));
 186 }
 187 
 188 # Convert a octal string into a hexadecimal string...
 189 #
 190 sub OctalToHexadecimal {
 191   my($Octal) = @_;
 192 
 193   return DecimalToHexadecimal(OctalToDecimal($Octal));
 194 }
 195 
 196 # Use Perl oct function to convert binary, octal or hexadecimal strings into decimal numbers.
 197 sub _ConvertToDecimal ($) {
 198   my($Value) = @_;
 199 
 200   return ($Value =~ /^0/) ? oct($Value) : $Value;
 201 }
 202