|
-
Jul 21st, 2008, 08:45 PM
#1
Thread Starter
Hyperactive Member
Converting Java snippet to VBA
I have the following Java code:
Code:
public static void main(String [ ] args)
{
String isbn = "9781590387474";
if (isbn.length() == 13 && isbn.indexOf("978") == 0)
{
isbn = isbn.substring(3,12);
int xsum = 0;
for (int i = 0; i < 9; i++)
{
xsum += (10 - i) * Character.getNumericValue(isbn.charAt(i));
}
xsum %= 11;
xsum = 11 - xsum;
String x_val = String.valueOf(xsum);
switch (xsum)
{
case 10: x_val = "X"; break;
case 11: x_val = "0"; break;
}
isbn += x_val;
}
System.out.println(isbn);
}
I tried converting it to VBA myself, and got this:
Code:
Function UPCtoISBN(strUPCin As String) As String
Dim strTemp As String
If Len(strUPCin) = 13 And Left(strUPCin, 3) = "978" Then
strTemp = Mid(strUPCin, 3, 10)
Dim xsum As Integer
xsum = 0
For x = 1 To 9
xsum = xsum + ((10 - x) * Mid(strTemp, x, 1))
Next x
xsum = xsum Mod 11
xsum = 11 - xsum
Select Case xsum
Case 10
strTemp = strTemp & "X"
Case 11
strTemp = strTemp & "0"
End Select
End If
UPCtoISBN = strTemp
End Function
Unfortunately, it does not work.
Anyone willing to help fix it?
Thanks!
-
Jul 23rd, 2008, 03:57 PM
#2
Thread Starter
Hyperactive Member
Re: Converting Java snippet to VBA
Well, I changed it to this:
Code:
Function UPCtoISBN(strUPCin As String) As String
Dim strTemp As String
If Len(strUPCin) = 13 And Left(strUPCin, 3) = "978" Then
strTemp = Mid(strUPCin, 4, 9)
Dim xsum As Integer
xsum = 0
For x = 1 To 9
xsum = xsum + ((10 - x + 1) * Mid(strTemp, x, 1))
Next x
xsum = xsum Mod 11
xsum = 11 - xsum
Select Case xsum
Case 10
strTemp = strTemp & "X"
Case 11
strTemp = strTemp & "0"
Case Else
strTemp = strTemp & xsum
End Select
End If
UPCtoISBN = strTemp
End Function
and it worked for all the books I checked.
Anyone care to try a few books of their own for me to validate?
Thanks!
-
Aug 11th, 2008, 12:40 AM
#3
Re: Converting Java snippet to VBA
It would be best to separate calculation of check digit, e.g. another package/class for check digit calculation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|