On Thu, Mar 07, 2002 at 03:07:29PM -0600, Russ Foster wrote: > Questoin #1: Is there a function or module to convert numbers to/from > decimal, binary, and hex?
Your question is a little vague. Do you have a number, and want to represent it in a certain base? Or do you have a string that should be interpreted as being in a certain base? For representing a number, $n, in hex use sprintf("%x", $n) octal use sprintf("%o", $n) decimal use $n binary use sprintf("%b", $n) The binary representation can be a little tricky; the %b sprintf specified wasn't added until 5.6.0, so in older versions of perl you have to use pack and unpack, e.g. unpack("B*", pack("C", $n)). The pack template has to change as $n gets larger than its template size, or $n has to be split up and each octet packed individually. You could also use a loop and vec. For interpreting a string, $str, in base hex use hex($str) octal use oct($str) decimal use $str binary use unpack("C", pack("B*", $str)) With the binary example the leading zeros in $str matter; 100 is 128, 00000100 is 4. In other words, zeros are appended, not prepended, in order to pad the bits out to a byte boundary. > Question #2: Is there a function or module to perform binary calculations > (AND, OR, XOR)? There are bitwise operators, & | and ^, covered in perldoc perlop. I'm assuming that's what you mean. Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com -- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]