Sunday, March 2, 2008

ISBN-13 to ISBN-10 Convertion

Recommended Conversion Tool: http://www.isbn.org/converterpub.asp


To convert an ISBN-13 to ISBN-10, you need not only to remove the first three characters. You also need to recompute the checksum.

Here is a piece of code that Febrice wrote:

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;
}

No comments: