有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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作为校验和。出于某种原因,新代码中的数字之和比应该的少了一个

我已经为此奋斗了很长一段时间,我相信我已经开始失明:我就是找不到我做错了什么。有人能帮忙吗


共 (4) 个答案

  1. # 1 楼答案

    这真的很容易。 请看我的JavaScript示例,了解转换的逻辑:

    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;
    } 
    
  2. # 2 楼答案

    您可以使用Apache commons validator库来完成这项工作。参见ISBNValidator::convertToISBN13方法

    import org.apache.commons.validator.routines.ISBNValidator;
    
    String isbn13 = ISBNValidator.getInstance().convertToISBN13("9513218589");
    
  3. # 3 楼答案

    给你

        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;
    }
    

    请原谅中间的日志行,我是从一个android项目复制粘贴的

  4. # 4 楼答案

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

    你将ISBN中的所有数字相加,包括加倍的数字

    例如:

    第7位->;双倍=14

    你在总数上加了14。应该是

    第7位->;双倍=14->;大于9?是的,所以1+4=5

    你应该加5