0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

私もisbn13をisbn10に変換する。

Last updated at Posted at 2019-06-15

配列インデックスと10からの重みを一致させといたらスッキリするかと、思ったら、そうでもなかったコードが以下です。

#追記
""scivola 2019-06-14 12:10 2 実使用する人がいるかもしれないので念のため。 13 桁版から 10 桁版に変換可能なのは「978」で始まっている ISBN だけです。 現在のところ,「978」と「979」が ISBN に使われています。""
とのこと。そーなのかー。暫定対応した。

isbn13= "9784797386295"; abort "Unsupported Number" if isbn13[0..2] != "978";
isbn13[3..-2] + (   (isbn13[3..-2]+"ZZ").split("").reverse.each_with_index.inject(0){|sum,(c,idx)| sum + (c.to_i * idx) }%11 * (-1)+11 ).to_s.gsub(/\d{2}/,{"10"=>"X","11"=>"0"})
=> "4797386290"

 isbn13="9784774183619";abort "Unsupported Number" if isbn13[0..2] != "978";
isbn13[3..-2] + (   (isbn13[3..-2]+"ZZ").split("").reverse.each_with_index.inject(0){|sum,(c,idx)| sum + (c.to_i * idx) }%11 * (-1)+11 ).to_s.gsub(/\d{2}/,{"10"=>"X","11"=>"0"})
=> "477418361X"
 isbn13="9784797386295";abort "Unsupported Number" if isbn13[0..2] != "978";
isbn13[3..-2] + (   (isbn13[3..-2]+"ZZ").split("").reverse.each_with_index.inject(0){|sum,(c,idx)| sum + (c.to_i * idx) }%11 * (-1)+11 ).to_s.gsub(/\d{2}/,{"10"=>"X","11"=>"0"})
=> "4797386290"

#参考
https://qiita.com/110416/items/9c627b2c06ae192ce586

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?