Thursday, September 21, 2006 12:28 AM bart

ISBN to EAN-13 (C#)

Introduction

Well, kind of a short follow-up to my previous post on EAN-13 codes. As I promised, I'd blog about the conversion from an ISBN code to the corresponding EAN-13 code. No rocket science after all, but because it doesn't have so much to do with EAN-13 by itself, I decided to put this stuff in a separate post.

The code

ISBN codes are codes of 10 digits (will grow to 13 digits in the near future). The first digit (or two) represent the country code (0 = US, 90 = Belgium), the next four (or three) digits are the publisher (321 = Addison-Wesley), the next four or five digits are a unique identifier for the publisher's book (15493 = The Common Language Infrastructure Annotated Standard) and the last one digit is the checksum digit.

A normal form for ISBN numbers

Quite often you see things like 0-321-15493-2 or 0 321 15493 2 in order to distinguish between the different "fields" composing the ISBN code. A first handy method sanitizes such a representation and turns it into a normal form:

static string NormalizeIsbn(string isbn)
{
   return isbn.Replace("-", "").Replace(" ", ""
);
}

Check the checksum

Next, what about the checksum? The 10th digit of the ISBN code is the checksum and is calculated as follows:

  1. Multiply the value of digits 1-9 (non zero-based indices) by their position (i.e. 1 to 9).
  2. Sum the results from step 1.
  3. Calculate the remainder after division by 11; if the result is 10, the checksum digit is 'X'.

An example:

 0   3   2   1   1   5   4   9   3   ?
 1   2   3   4   5   6   7   8   9
--  --  --  --  --  --  --  --  --

 0 + 6 + 6 + 4 + 5 +30 +28 +72 +27 = 178 (% 11 = 2)

The code to check the checksum can be found below:

static bool CheckIsbn(string isbn)
{
   if (isbn == null
)
      return false
;

   isbn = NormalizeIsbn(isbn);
   if
(isbn.Length != 10)
      return false
;

   int
res;
   for (int
i = 0; i < 9; i++)
      if (!int.TryParse(isbn[i].ToString(), out
res))
         return false
;

   int
sum = 0;
   for (int
i = 0; i < 9; i++)
      sum += (i + 1) *
int
.Parse(isbn[i].ToString());

   int
r = sum % 11;
   if
(r == 10)
      return isbn[9] == 'X'
;
   else
      return isbn[9] == (char)('0'
+ r);
}

Note: An example of checksum 'X' is the ISBN code 0-596-00351-X of "Shared Source CLI Essentials" (David Stutz, Ted Neward, Geoff Shilling - O'Reilly).

ISBN-to-EAN13

The last step: conversion from ISBN to EAN-13 for barcode representation. It's fairly easy:

  1. Drop the ISBN checksum digit.
  2. Append 978 in front of the remainder of the code (978 = ISBN Bookland Code)
  3. Calculate the EAN-13 checksum.

Notice this drops the possible 'X' in the code (which isn't representable in EAN-13). Here's the conversion code:

static string IsbnToEan13(string isbn)
{
   isbn = NormalizeIsbn(isbn);
   string code = "978"
+ isbn.Substring(0, 9);
   code += (
char)('0' + Ean13
.CalculateChecksum(code));
   return
code;
}

See my previous post for the definition of Ean13::CalculcateChecksum. Of course exposing the ISBN (or EAN-13) code generator through the web using .ashx handlers in ASP.NET is also possible. Follow the instructions on An ASP.NET .ashx HTTP handler for Code 39 barcode generation for more information (just replace all Code 39 stuff by Ean13 stuff).

Happy (bar)coding!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:

Comments

# Creating Barcodes On The Fly With C#

Monday, October 02, 2006 10:06 PM by Tom's corner

# Creating Barcodes On The Fly With C#

Friday, October 06, 2006 6:04 AM by It's Way Too Early For This

I had a couple of pointers to two of these articles, but here is the whole catalog of barcode code from