B4J Code Snippet ISBN Number generator

Hi everybody,
ISBN stands for the International Standard Book Number that is carried by almost each every book.
The following B4J short program is with a class that generate an ISBN number and an inline Java code that check if the number is ISBN.
The code can be used in all B4X platforms..
I hope you like it ..

B4X:
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    Log("Generating an ISBN number:")
    Dim isbnx As isbn
    isbnx.Initialize
    Log(isbnx.makeISBNNumber())
    Dim jo As JavaObject =Me
    Log(jo.RunMethod("checkISBNNumber",Array(7771233457)))
    StartMessageLoop
End Sub
#if JAVA

  public static boolean checkISBNNumber(long number) 
   { 
   int sum = 0; 
   int i, t, intNumber, dNumber; 
   String strNumber; 
        
   strNumber = ""+number; 
        
   if (strNumber.length() != 10) { 
                   return false; 
           } 
        
           for (i = 0; i < strNumber.length(); i++) { 
                   intNumber = Integer.parseInt(strNumber.substring(i, i+1)); 
                   dNumber = i + 1; 
                   t = dNumber * intNumber; 
                   sum = sum + t; 
           } 
 
           // check whether the sum is divisible by 11 or not 
      
           if ((sum % 11) == 0) { 
                   return true; 
           } 
        
   return false; 
        
   } 
#End If

Class isbn:
Sub Class_Globals
    
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    '
End Sub

Public Sub makeISBNNumber() As String
 
    Dim isbn, a, b, c, d, e, f, g, h, i, j As Int
    Private x, n As Int
    For x = 0 To 99
    
    a = Rnd(1,9)
    b = Rnd(1,9)
    c = Rnd(1,9)
    d = Rnd(1,9)
    e = Rnd(1,9)
    f = Rnd(1,9)
    g = Rnd(1,9)
    h = Rnd(1,9)
    i = Rnd(1,9)
    j = Rnd(1,9)
    isbn = (a + (b * 2) + (c * 3) + (d * 4) + (e * 5) + (f * 6) + (g * 7) + (h * 8) + (i * 9) + (j * 10))
    
    If isbn Mod 11 = 0 Then
            
        Return ($"${a}${b}${c}${d}${e}${f}${g}${h}${i}${j}"$)
      n = n + 1
    End If
    Next

    
End Sub
 

Sandman

Expert
Licensed User
Longtime User
Sorry, but I don't think that's how ISBN work. You might have produced something with the same format as ISBN, but it's not an ISBN number. Those are issued by an ISBN organization in each country. It's not a number that we can just pick out of thin air.

This link explains more how ISBN numbers are issued:

And here is the complete list of all organizations over the world that issue ISBN numbers:
 

jkhazraji

Active Member
Licensed User
Longtime User
Sorry, but I don't think that's how ISBN work. You might have produced something with the same format as ISBN, but it's not an ISBN number. Those are issued by an ISBN organization in each country. It's not a number that we can just pick out of thin air.

This link explains more how ISBN numbers are issued:

And here is the complete list of all organizations over the world that issue ISBN numbers:
Please see:

There must be a way to check whether the number is ISBN or not even if it is out of the thin air.
and Also see:
 

Sandman

Expert
Licensed User
Longtime User
There must be a way to check whether the number is ISBN or not even if it is out of the thin air.
Yeah, perhaps. Assuming that the International ISBN Organization, or some other organization, have made an API available where one can look up a number and get feedback regarding whether it exists or not. Assuming that's true (which I kind of doubt, but let's assume) - now what?

Using the check digit is a very shallow method of checking an ISBN number that doesn't give you anything beyond than the most basic validation and thus doesn't really provide a whole lot of value.

I'm not trying to piss on your solution, I just want to point out that it's a larger, and more complex, system.

What was the purpose of your initial code? So that people and organizations publishing books could get an ISBN number and put on the cover? I'm sorry, that won't work. The official way to make ISBN numbers seems to be to use the official ISBN Calculator, which one would get by contacting the national ISBN agency:
 

jkhazraji

Active Member
Licensed User
Longtime User
Yeah, perhaps. Assuming that the International ISBN Organization, or some other organization, have made an API available where one can look up a number and get feedback regarding whether it exists or not. Assuming that's true (which I kind of doubt, but let's assume) - now what?

Using the check digit is a very shallow method of checking an ISBN number that doesn't give you anything beyond than the most basic validation and thus doesn't really provide a whole lot of value.

I'm not trying to piss on your solution, I just want to point out that it's a larger, and more complex, system.

What was the purpose of your initial code? So that people and organizations publishing books could get an ISBN number and put on the cover? I'm sorry, that won't work. The official way to make ISBN numbers seems to be to use the official ISBN Calculator, which one would get by contacting the national ISBN agency:
Sir,
What do you suggest then? Shall I delete it?
 

Sandman

Expert
Licensed User
Longtime User
Sir,
What do you suggest then? Shall I delete it?
Oh, no, no, don't do that! This thread still holds value that other forum members will be thankful for. :) You should instead be proud that you made something that you wanted to share on the forum - that's what makes this forum awesome.The only mistake you did was that you didn't have any previous knowledge about ISBN, so naturally it's simple to assume one can just make it like this. Very understandable mistake to make, not something you should feel bad about in any way.

And also, you did make some nice code there for the checksum. If I were you I would make that checksum code into a new thread and post code for how to generate a checksum number, and validate it also. (And link to the good WIkipedia page you found above.)
 

Knoppi

Active Member
Licensed User
Longtime User
I found this in one of my old libraries
B4X:
Public Sub CheckISBN10( Isbn As String) As Boolean
    Isbn = Isbn.Replace( "-", "")
    If Isbn.Length <> 10 Then Return False
    Dim sum As Int = 0
    For i=0 To Isbn.Length -1
        Dim n As String = Isbn.CharAt(i)
        If n.ToLowerCase = "x" Then n = "10"
        Dim d As Int = n *(i+1)
        sum = sum + d
    Next
    Return ( sum Mod 11 = 0)
End Sub

'if Checksum = 10 then Checksum = "x"
Public Sub ChecksumISBN10( Isbn As String) As Int
    Isbn = Isbn.Replace( "-", "")
    If Isbn.Length <> 9 Then Return -1
    Dim sum As Int = 0
    For i=0 To Isbn.Length -1
        Dim n As String = Isbn.CharAt(i)
        Dim d As Int = n *(i+1)
        sum = sum + d
    Next
    Return sum Mod 11
End Sub

Public Sub CheckISBN13( Isbn As String) As Boolean
    Isbn = Isbn.Replace( "-", "")
    If Isbn.Length <> 13 Then Return False
    Dim n1 As Int = 0
    Dim n3 As Int = 0
    Dim n As String
    For i=0 To Isbn.Length -1
        If (i+1) Mod 2 = 0 Then
            n = Isbn.CharAt( i)
            n3 = n3 + n
        Else
            n = Isbn.CharAt( i)
            n1 = n1 + n
        End If
    Next
    Return (( n1 + n3 *3) Mod 10 = 0)
End Sub

Public Sub ChecksumISBN13( isbn As String) As Int
    isbn = isbn.Replace( "-", "")
    If isbn.Length <> 12 Then Return -1
    Dim n1 As Int = 0
    Dim n3 As Int = 0
    Dim n As String
    For i=0 To isbn.Length -1
        If (i+1) Mod 2 = 0 Then
            n = isbn.CharAt( i)
            n3 = n3 + n
        Else
            n = isbn.CharAt( i)
            n1 = n1 + n
        End If
    Next
    Return 10 - (( n1 + n3 *3) Mod 10)
End Sub
 

jkhazraji

Active Member
Licensed User
Longtime User
I found this in one of my old libraries
B4X:
Public Sub CheckISBN10( Isbn As String) As Boolean
    Isbn = Isbn.Replace( "-", "")
    If Isbn.Length <> 10 Then Return False
    Dim sum As Int = 0
    For i=0 To Isbn.Length -1
        Dim n As String = Isbn.CharAt(i)
        If n.ToLowerCase = "x" Then n = "10"
        Dim d As Int = n *(i+1)
        sum = sum + d
    Next
    Return ( sum Mod 11 = 0)
End Sub

'if Checksum = 10 then Checksum = "x"
Public Sub ChecksumISBN10( Isbn As String) As Int
    Isbn = Isbn.Replace( "-", "")
    If Isbn.Length <> 9 Then Return -1
    Dim sum As Int = 0
    For i=0 To Isbn.Length -1
        Dim n As String = Isbn.CharAt(i)
        Dim d As Int = n *(i+1)
        sum = sum + d
    Next
    Return sum Mod 11
End Sub

Public Sub CheckISBN13( Isbn As String) As Boolean
    Isbn = Isbn.Replace( "-", "")
    If Isbn.Length <> 13 Then Return False
    Dim n1 As Int = 0
    Dim n3 As Int = 0
    Dim n As String
    For i=0 To Isbn.Length -1
        If (i+1) Mod 2 = 0 Then
            n = Isbn.CharAt( i)
            n3 = n3 + n
        Else
            n = Isbn.CharAt( i)
            n1 = n1 + n
        End If
    Next
    Return (( n1 + n3 *3) Mod 10 = 0)
End Sub

Public Sub ChecksumISBN13( isbn As String) As Int
    isbn = isbn.Replace( "-", "")
    If isbn.Length <> 12 Then Return -1
    Dim n1 As Int = 0
    Dim n3 As Int = 0
    Dim n As String
    For i=0 To isbn.Length -1
        If (i+1) Mod 2 = 0 Then
            n = isbn.CharAt( i)
            n3 = n3 + n
        Else
            n = isbn.CharAt( i)
            n1 = n1 + n
        End If
    Next
    Return 10 - (( n1 + n3 *3) Mod 10)
End Sub
Thanks.. that is great!.. I used your subs to check the generated ISBN number..
Log(isbnx.CheckISBN10(isbnumx)):
Log(isbnx.CheckISBN10(isbnumx))
It gave true..
Allow me to add your subs to the class
here is an updated version
 

Attachments

  • isbnumV100.zip
    2 KB · Views: 188

Beja

Expert
Licensed User
Longtime User
Good work, but still not enough to just check availability and produce a new ISBN number because only authorized agencies can produce and sell an ISBN number.
No one can produce car VIN numbers, and this number is for each book in the world, is xactly the same as a VIN unique number of each car
in the world. A car maker buys a bulk of VIN numbers from an official authority in each country and prints them on the cars' chases. or windshield Otherwise
you may have more than one car with the same VIN number. Bowker is the official United States and its territories agent for providing ISBN
numbers and barcodes for authors and book publishers. https://www.bowker.com/
 
Last edited:

Derek Johnson

Active Member
Licensed User
Longtime User
There are some more restrictions on valid ISBNs:

ISBN-10, the previous version of ISBN, had a 10-digit format and the first digit could be any number from 0 to 9.

ISBN-13, the current version of ISBN, has a 13-digit format and the first three digits must always be 978 or 979. This is known as the "Bookland" prefix, and it is used to indicate that the ISBN is part of the Bookland EAN. The next 9 digits can be any number from 0 to 9.

In summary, ISBN-10 is 10 digits long and ISBN-13 is 13 digits long, ISBN-13 starts with 978 or 979.
 
Top