Math-BaseCalc-1.011 > Math::BaseCalc

= pod

名前

Math::BaseCalc - 様々な基数間で数値を変換するモジュール

概要

  use Math::BaseCalc;

  my $calc = new Math::BaseCalc(digits => [0,1]); # 2 進数
  my $bin_string = $calc->to_base(465); # 465 を 2 進数に変換

  $calc->digits('oct'); # 8 進数
  my $number = $calc->from_base('1574'); # 8 進数の 1574 を 10 進数に変換

説明

This module facilitates the conversion of numbers between various number bases. You may define your own digit sets, or use any of several predefined digit sets.

このモジュールは、様々な基数間の数値変換を容易にするものです。 あらかじめ用意された様々な基数の文字セットを使うこともできますし、 任意の基数の文字セットを定義することもできます。

訳注: 「digit set」を「文字セット」と訳しています。「digit set」 とは、通常用いられる 16 進数で言えば「0-9a-f」のことです。 巧い表現や慣例的に使われている表現があれば指摘してください。

The to_base() and from_base() methods convert between Perl numbers and strings which represent these numbers in other bases. For instance, if you're using the binary digit set [0,1], $calc->to_base(5) will return the string "101". $calc->from_base("101") will return the number 5.

to_base() メソッドと from_base() メソッドは、Perl での数値と 他の基数の数値を表す文字列の間で変換します。例えば、2 進数を表す 文字セットとして [0,1] を用いたならば、$calc->to_base(5)は文字列 "101" を 返します。$calc->from_base("101") は数値を返します。

訳注:変な訳だ。英文と具体例をみて理解してください。:-)

To convert between, say, base 7 and base 36, use the 2-step process of first converting to a Perl number, then to the desired base for the result:

7 進数と 36 進数の間で変換するためには、最初に Perl の数値に変換し、 その次に先の結果を希望する基数に変換するという 2 段階のプロセスを 経る必要があります。:

 $calc7  = new Math::BaseCalc(digits=>[0..6]);
 $calc36 = new Math::BaseCalc(digits=>[0..9,'a'..'z'];
 
 $in_base_36 = $calc36->to_base( $calc7->from_base('3506') );

If you just need to handle regular octal & hexdecimal strings, you probably don't need this module. See the sprintf(), oct(), and hex() Perl functions.

もし、標準的な 8 進数や 16 進数の文字列を扱う必要があるならば、 このモジュールを用いる必要はありません。 sprintf() や oct()、 hex() などの Perl の関数を参照してください。

メソッド

  • new Math::BaseCalc

  • new Math::BaseCalc(digits=>...)

    Create a new base calculator. You may specify the digit set to use, by either giving the digits in a list reference (in increasing order, with the 'zero' character first in the list) or by specifying the name of one of the predefined digit sets (see the digit() method below).

    新しい基数計算のできる電卓(訳注:Math::BaseCalc オブジェクト)を 作成します。使いたい文字セットを(はじめが'0'である昇順に並べられた) リストのリファレンスとして定義するか、あらかじめ用意されている 文字セットを使うことを指定できます。 (後者に関しては digit() メソッドを見てください。)

  • $calc->to_base(NUMBER)

    Converts a number to a string representing that number in the associated base.

    数値を、指示されている基数として文字列で表現された数値へ変換します。

  • $calc->from_base(STRING)

    Converts a string representing a number in the associated base to a Perl integer. The behavior when fed strings with characters not in $calc's digit set is currently undefined.

    指示されている基数として文字列で表現された数値を、Perl の整数に 変換します。$calc の文字セットとして定義されていない文字列を 与えたときの振る舞いは、今は定義されていません。

  • $calc->digits

  • $calc->digits(...)

    Get/set the current digit set of the calculator. With no arguments, simply returns a list of the characters that make up the current digit set. To change the current digit set, pass a list reference containing the new digits, or the name of a predefined digit set. Currently the predefined digit sets are:

    電卓(訳注:Math::BaseCalc オブジェクト)の文字セットを get/set します。 引数無しの場合は、単に現在の文字セットを構築する文字のリストを返します。 現在の文字セットを変更するためには、新しい文字セットのリストリファレンスか、 若しくは、あらかじめ用意されている文字セットの名前を(引数として) 渡してください。あらかじめ用意されている文字セットの一覧は次の通りです。

           bin => [0,1],
           hex => [0..9,'a'..'f'],
           HEX => [0..9,'A'..'F'],
           oct => [0..7],
           64  => ['A'..'Z','a'..'z',0..9,'+','/'],
           62  => [0..9,'a'..'z','A'..'Z'],
     
     Examples:
      $calc->digits('bin');
      $calc->digits([0..7]);
      $calc->digits([qw(w a l d o)]);

    If any of your "digits" has more than one character, the behavior is currently undefined.

    あなたの文字セットが複数の文字を持っている場合の振る舞いは 現在定義されていません。

QUESTIONS

The '64' digit set is meant to be useful for Base64 encoding. I took it from the MIME::Base64.pm module. Does it look right? It's sure in a strange order.

'64' という文字セットは Base64 符号化に役に立つと考えています。これは、 MIME::Base64.pm モジュールからいただきました。どう思いますか? 確かに奇妙だとは思っています。

作者

Ken Williams, ken@forum.swarthmore.edu

コピーライト

This is free software in the colloquial nice-guy sense of the word. Copyright (c) 1999, Ken Williams. You may redistribute and/or modify it under the same terms as Perl itself.

SEE ALSO

perl(1).

翻訳について

翻訳者:IWAI, Masaharu <iwaim.sub@gmail.com>

Perlドキュメント日本語訳 Project にて、 Perlモジュール、ドキュメントの翻訳を行っております。

 http://sourceforge.jp/projects/perldocjp/
 http://www.freeml.com/ctrl/html/MLInfoForm/perldocjp@freeml.com
 http://perldoc.jp/