Fabrice's weblog

Tools and Source

News


Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET

The views expressed on this weblog are mine alone and do not necessarily reflect the views of my employer. The content of this weblog is independent from Microsoft or any other company. transatlantys hot news

Contact

Me

Others

Selected content

ISBN-13 to ISBN-10

If for some reason you need to convert an ISBN-13 to ISBN-10 (one being that Amazon doesn't support ISBN-13 in product affiliate links) then you need not only to remove the first three characters. You also need to recompute the checksum.

Here is a piece of code that I wrote to handle this conversion:

public static String Isbn13to10(String isbn13)
{
  if (String.IsNullOrEmpty(isbn13))
    throw new ArgumentNullException("isbn13");
  isbn13 = isbn13.Replace("-", "").Replace(" ", "");
  if (isbn13.Length != 13)
    throw new ArgumentException("The ISBN doesn't contain 13 characters.", "isbn13");

  String isbn10 = isbn13.Substring(3, 9);
  int checksum = 0;
  int weight = 10;

  foreach (Char c in isbn10)
  {
    checksum += (int)Char.GetNumericValue(c) * weight;
    weight--;
  }

  checksum = 11-(checksum % 11);
  if (checksum == 10)
    isbn10 += "X";
  else if (checksum == 11)
    isbn10 += "0";
  else
    isbn10 += checksum;

  return isbn10;
}


I use this to create the Amazon affiliate links on Clair de Bulle, the famous French comics website of my wife. Yes, it's the holidays season, so I have to work for her ;-)

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)