Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting ISBN10 to ISBN13

I have tried to convert ISBN10 codes to ISBN13 numbers with Java. From . On isbn-13.info I found the way to convert them.

Example: 0-123456-47-9

  • Begin with prefix of “978”
  • Use the first nine numeric characters of the ISBN (include dashes) 978-0-123456-47-
  • Calculate the EAN check digit using the “Mod 10 Algorithm” 978-0-123456-47-2

Using that I have created a Java program to do the conversion.

public class ISBNConverter {
    public static void main(String[] args) {
        String isbn10 = "9513218589";
        String isbn13 = "";
        int sum = 0;
        int checkNumber = 0;
        int multiplier = 2;

        String code = "978" + isbn10.substring(0, isbn10.length() - 1);

        for(int i = code.length() - 1; i >= 0; i--) {
            int num = Character.getNumericValue(code.charAt(i));
            isbn13 += String.valueOf(num * multiplier);

            multiplier = (multiplier == 2) ? 1 : 2;
        }

        for(int i = 0; i < isbn13.length(); i++) {
            sum += Character.getNumericValue(isbn13.charAt(i));
        }

        while(sum % 10 != 0) {
            sum++;
            checkNumber++;
        }

        System.out.println(checkNumber);
    }
}

For the example ISBN10 code 9513218589 (978951321858 ISBN13 without the check number) it returns 5 as the check number. If I calculate it using the converter on ISBN's official site I get 4 as the check sum. For some reason, the sum of the numbers in the new code is one less than it should be.

I have being fighting with this for a long time and I believe I have began blind: I just can't find what I'm doing wrong. Could someone help with this?

like image 227
MikkoP Avatar asked Jun 14 '13 12:06

MikkoP


People also ask

What number is isbn13?

The International Standard Book Number (ISBN) is a numeric commercial book identifier that is intended to be unique. Publishers purchase ISBNs from an affiliate of the International ISBN Agency. International Standard Book Number. A 13-digit ISBN, 978-3-16-148410-0, as represented by an EAN-13 bar code. Acronym.

How do I decode an ISBN number?

Interpreting a 13 Digit ISBN. Look at the first three numbers to establish when the book was published. The first three numbers are a prefix that changes overtime. Since the implementation of the 13 digit ISBN, this series has only ever been "978" or "979."

How do I find the 10 digit ISBN?

Its ISBN, shown as a simple string of 10 digits, is 0198526636. When you look at the ISBN as printed on the book shown in the photographs taken of the back cover above the barcode and from the back of the title page near the front of the book, you can see it is separated into sections by hyphens.


2 Answers

Here you go

    public static String ISBN10toISBN13( String ISBN10 ) {
    String ISBN13  = ISBN10;
    ISBN13 = "978" + ISBN13.substring(0,9);
    //if (LOG_D) Log.d(TAG, "ISBN13 without sum" + ISBN13);
    int d;

    int sum = 0;
    for (int i = 0; i < ISBN13.length(); i++) {
        d = ((i % 2 == 0) ? 1 : 3);
        sum += ((((int) ISBN13.charAt(i)) - 48) * d);
        //if (LOG_D) Log.d(TAG, "adding " + ISBN13.charAt(i) + "x" + d + "=" + ((((int) ISBN13.charAt(i)) - 48) * d));
    }
    sum = 10 - (sum % 10);
    ISBN13 += sum;

    return ISBN13;
}

pardon the log lines in between, I am copy pasting it from an android project i am working on

like image 196
Arnav Gupta Avatar answered Sep 22 '22 01:09

Arnav Gupta


In the

for(int i = 0; i < isbn13.length(); i++) { sum += Character.getNumericValue(isbn13.charAt(i)); }

You're adding up all the digits from the ISBN, including the doubled ones.

Example:

digit 7 -> double = 14

You're adding 14 to the sum. Is should be

digit 7 -> double = 14 -> bigger than 9? yes, so 1+4 = 5

and you should add 5.

like image 36
woliveirajr Avatar answered Sep 26 '22 01:09

woliveirajr