java将ISBN10转换为ISBN13
我试着用Java将ISBN10代码转换成ISBN13数字。从…起在isbn-13.info上,我找到了转换它们的方法
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
使用它,我创建了一个Java程序来进行转换
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);
}
}
检查{cdbn5}返回不带代码的数字。如果我用ISBN's official site上的转换器计算它,我得到4作为校验和。出于某种原因,新代码中的数字之和比应该的少了一个
我已经为此奋斗了很长一段时间,我相信我已经开始失明:我就是找不到我做错了什么。有人能帮忙吗
# 1 楼答案
这真的很容易。 请看我的JavaScript示例,了解转换的逻辑:
# 2 楼答案
您可以使用Apache commons validator库来完成这项工作。参见ISBNValidator::convertToISBN13方法
# 3 楼答案
给你
请原谅中间的日志行,我是从一个android项目复制粘贴的
# 4 楼答案
在
你将ISBN中的所有数字相加,包括加倍的数字
例如:
第7位->;双倍=14
你在总数上加了14。应该是
第7位->;双倍=14->;大于9?是的,所以1+4=5
你应该加5