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?