8

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?

MikkoP
  • 4,864
  • 16
  • 58
  • 106

4 Answers4

6

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

Arnav Gupta
  • 916
  • 12
  • 14
3

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.

woliveirajr
  • 9,433
  • 1
  • 39
  • 49
2

You could use the Apache commons-validator library to do this for you. See the ISBNValidator::convertToISBN13 method.

import org.apache.commons.validator.routines.ISBNValidator;

String isbn13 = ISBNValidator.getInstance().convertToISBN13("9513218589");
zman0900
  • 379
  • 2
  • 12
-1

It's really easy. Please look at my JavaScript example to understand the logic of conversion:

function isbn13to10(isbn13) {
    var digits = [];
    var sum = 0; var chk_tmp, chk_digit;

    digits = (isbn13 + "").substr(3,9).split("") ;

    for(var i = 0; i < 9; i++) {
        sum += digits[i] * (10 - i);
    }

    chk_tmp = 11 - (sum % 11);

    if (chk_tmp == 10) {
        chk_digit = 'x';
    } else if (chk_tmp == 11) {
        chk_digit = 0;
    } else {
        chk_digit = chk_tmp;
    }

    digits.push(chk_digit);

    return digits.join("");
}


function isbn10to13(isbn10){
    var sum = (isbn10 + "").charAt(9); 
    var mltp = 0;
    var total = 0;

    if (sum == "X") { sum = 10; }

    isbn10 = "978"+isbn10.substring(0,9);

    for (i=0; i<12; i++) {
        mltp = (i % 2) == 0 ? 1 : 3;
        total = total+(isbn10.charAt(i)*mltp);
    }       
    sum = (10 - (total % 10)) % 10;

    return isbn10+sum;
} 
Denys Rusov
  • 560
  • 6
  • 6