Ruby 2.2.0 リファレンスマニュアル > ライブラリ一覧 > opensslライブラリ > OpenSSL::BNクラス > to_s

instance method OpenSSL::BN#to_s

to_s(base=10) -> String[permalink][rdoc]

自身を表す文字列を返します。

base で、変換方法(基数)を指定します。デフォルトは 10 で、他に 16, 2, 0 を指定できます。

10  10進数の表記
16  16進数の表記
 2  big-endianの符号無し整数のバイナリ列
 0  MPI形式の文字列(バイト列)
[PARAM] base:
文字列への変換方法(基数)
[EXCEPTION] OpenSSL::BNError:
変換に失敗した場合に発生します

require 'openssl'

p 10.to_bn.to_s   # =>  "10"
p (-5).to_bn.to_s # =>  "-5"

p 0.to_bn.to_s(16)  # =>   "0"
p 9.to_bn.to_s(16)  # =>  "09"
p 10.to_bn.to_s(16) # =>  "0A"
p 16.to_bn.to_s(16) # =>  "10"
p 26.to_bn.to_s(16) # =>  "1A"
p 256.to_bn.to_s(16) # => "0100"

p 0.to_bn.to_s(2) # => ""
p 6.to_bn.to_s(2) # => "\x06"
p 7.to_bn.to_s(2) # => "\a"

p 0.to_bn.to_s(0) # => "\x00\x00\x00\x00"
p 6.to_bn.to_s(0) # => "\x00\x00\x00\x01\x06"
p 7.to_bn.to_s(0) # => "\x00\x00\x00\x01\a"

反対に、文字列から OpenSSL::BN クラスのインスタンスを作るには OpenSSL::BN.new を用います。

[SEE_ALSO] OpenSSL::BN.new