guinness Posted January 11, 2015 Share Posted January 11, 2015 (edited) The following code checks if a string contains a valid 10-digit or 13-digit ISBN number. Enjoy!Function: expandcollapse popup#include <StringConstants.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: IsISBN ; Description ...: Check if a string contains a valid ISBN number. ; Syntax ........: IsISBN($sISBN, $iType) ; Parameters ....: $sISBN - A string value containing an ISBN number. ; $iType - An integer value or either 10 or 13, depending on the ISBN type to check. ; Return values .: Success: True ; Failure: False and sets @error to non-zero on error: ; 1 = String was empty or contained only whitespace. ; 2 = Type was incorrect. ; 3 = StringToASCIIArray() was empty. ; Author ........: guinness ; Link ..........: https://en.wikipedia.org/wiki/International_Standard_Book_Number ; Example .......: Yes ; =============================================================================================================================== Func IsISBN($sISBN, $iType) If Not StringStripWS($sISBN, $STR_STRIPALL) Then Return SetError(1, 0, False) Local Const $iType10 = 10, $iType13 = 13 $iType = Int($iType) If $iType <> $iType10 And $iType <> $iType13 Then Return SetError(2, 0, False) Local $aArray = StringToASCIIArray($sISBN) Local $iLength = UBound($aArray) - 1 If $iLength < 0 Then Return SetError(3, 0, False) Local Const $iNine = 57, $iZero = 48 Local $iCounter = 0, $iSum = 0 Switch $iType Case $iType10 If $aArray[$iLength] = 88 Or $aArray[$iLength] = 120 Then $iLength -= 1 $iSum = 10 EndIf $iCounter = 10 For $i = 0 To $iLength If $aArray[$i] < $iZero Or $aArray[$i] > $iNine Then ContinueLoop EndIf $iSum += ($aArray[$i] - $iZero) * $iCounter $iCounter -= 1 Next Return Mod($iSum, 11) = 0 ; Divisible by 11. Case $iType13 Local Const $iOne = 1, $iThree = 3 $iCounter = $iOne For $i = 0 To $iLength If $aArray[$i] < $iZero Or $aArray[$i] > $iNine Then ContinueLoop EndIf $iSum += ($aArray[$i] - $iZero) * $iCounter $iCounter = ($iCounter = $iOne) ? $iThree : $iOne Next Return Mod($iSum, 10) = 0 ; Divisible by 10. EndSwitch Return False EndFunc ;==>IsISBN Example use of Function: Example() Func Example() Local Const $iType10 = 10, $iType13 = 13 ; 10-digit ISBN numbers. PrintISBN('ISBN0-9752298-0-X', $iType10, True) ; Is a 10-digit ISBN number. PrintISBN('ISBN0-9752298-0-4', $iType10, False) ; Is not a 10-digit ISBN number. PrintISBN('ISBN1-84356-028-3', $iType10, True) ; Is a 10-digit ISBN number. PrintISBN('ISBN0-19-852663-6', $iType10, True) ; Is a 10-digit ISBN number. PrintISBN('ISBN 1 86197 271 7', $iType10, True) ; Is a 10-digit ISBN number. PrintISBN('ISBN 4 86197 271 7', $iType10, False) ; Is not a 10-digit ISBN number. ConsoleWrite(@CRLF) ; Empty line. ; 13-digit ISBN numbers. PrintISBN('ISBN-13: 978-0-306-40615-7', $iType13, True) ; Is a 13-digit ISBN number. PrintISBN('ISBN-13: 978-1-86197-876-9', $iType13, True) ; Is a 13-digit ISBN number. PrintISBN('ISBN-13: 978-1-86197-876-8', $iType13, False) ; Is not a 13-digit ISBN number. EndFunc ;==>Example Func PrintISBN($sISBN, $iType, $bExpected) ConsoleWrite($sISBN & ' => ' & IsISBN($sISBN, $iType) & ' [Expected: ' & $bExpected & ']' & @CRLF) EndFunc ;==>PrintISBN Note: C# variation can be found >here.Note: TypeScript/JavaScript can be found >here. Edited January 12, 2015 by guinness UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Exit Posted January 11, 2015 Share Posted January 11, 2015 Example is missing the include file. App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
guinness Posted January 11, 2015 Author Share Posted January 11, 2015 Example is missing the include file.It's on purpose, just copy the function above the example in SciTE. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
guinness Posted January 11, 2015 Author Share Posted January 11, 2015 (edited) ...and my AutoIt version ported to C#. expandcollapse popup#region Required project assemblies using System; #endregion namespace IsISBN { internal class Program { /// <summary> /// ISBN related flags. /// </summary> private enum ISBN : byte { /// <summary> /// 10-digit ISBN. /// </summary> Ten, /// <summary> /// 13-digit ISBN. /// </summary> Thirteen } /// <summary> /// Check if a string contains a valid ISBN number. /// </summary> /// <param name="isbn">A string value containing an ISBN number.</param> /// <param name="type">An ISBN flag of the type to check.</param> /// <returns>true if a valid ISBN number; otherwise, false.</returns> private static bool IsISBN(string isbn, ISBN type) { if (String.IsNullOrWhiteSpace(isbn)) { return false; } int length = isbn.Length - 1; int counter, sum = 0; switch (type) { case ISBN.Ten: { if (isbn[length] == 'x' || isbn[length] == 'X') { length -= 1; sum = 10; } counter = 10; for (int i = 0; i <= length; i++) { if (isbn[i] < '0' || isbn[i] > '9') { continue; } sum += (isbn[i] - '0') * counter; counter -= 1; } return sum % 11 == 0; // Divisible by 11. } case ISBN.Thirteen: { const int one = 1, three = 3; counter = one; for (int i = 0; i <= length; i++) { if (isbn[i] < '0' || isbn[i] > '9') { continue; } sum += (isbn[i] - '0') * counter; counter = (counter == one) ? three : one; } return sum % 10 == 0; // Divisible by 10. } } return false; } private static void Main() { Action<string, ISBN, bool> PrintISBN = // Anonymous method. (isbn, type, isExpected) => Console.WriteLine(isbn + " => " + IsISBN(isbn, type) + " [Expected: " + isExpected + "]"); // 10-digit ISBN numbers. PrintISBN("ISBN0-9752298-0-X", ISBN.Ten, true); // Is a 10-digit ISBN number. PrintISBN("ISBN0-9752298-0-4", ISBN.Ten, false); // Is not a 10-digit ISBN number. PrintISBN("ISBN1-84356-028-3", ISBN.Ten, true); // Is a 10-digit ISBN number. PrintISBN("ISBN0-19-852663-6", ISBN.Ten, true); // Is a 10-digit ISBN number. PrintISBN("ISBN 1 86197 271 7", ISBN.Ten, true); // Is a 10-digit ISBN number. PrintISBN("ISBN 4 86197 271 7", ISBN.Ten, false); // Is not a 10-digit ISBN number. Console.WriteLine(); // Empty line. // 13-digit ISBN numbers. PrintISBN("ISBN-13: 978-0-306-40615-7", ISBN.Thirteen, true); // Is a 13-digit ISBN number. PrintISBN("ISBN-13: 978-1-86197-876-9", ISBN.Thirteen, true); // Is a 13-digit ISBN number. PrintISBN("ISBN-13: 978-1-86197-876-8", ISBN.Thirteen, false); // Is not a 13-digit ISBN number. Console.WriteLine("Press any key to continue . . ."); Console.ReadKey(true); } } } Edited January 11, 2015 by guinness UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
guinness Posted January 11, 2015 Author Share Posted January 11, 2015 (edited) ...and my AutoIt version ported to TypeScript. expandcollapse popupfunction IsISBN(isbn: string, type: number): boolean { if (isbn.length == 0) { return false; } var type10: number = 10, type13: number = 13; if (type != type10 && type != type13) { return false; } var length = isbn.length - 1; var nine: number = 57, zero: number = 48; var char: number, counter: number = 0, sum: number = 0; switch (type) { case type10: { char = isbn[length].charCodeAt(0) if (char == 88 || char == 120) { length--; sum = 10; } counter = 10; for(var i: number = 0;i <= length ;i++) { char = isbn[i].charCodeAt(0); if (char < zero || char > nine) { continue; } sum += (char - zero) * counter; counter -= 1; } return sum % 11 == 0; } case type13: { var one: number = 1, three: number = 3; counter = one; for(var i: number = 0;i <= length ;i++) { char = isbn[i].charCodeAt(0); if (char < zero || char > nine) { continue; } sum += (char - zero) * counter; counter = (counter == one) ? three : one; } return sum % 10 == 0; } } return false; } var result = IsISBN("ISBN-13: 978-1-86197-876-9", 13); document.write("ISBN-13: 978-1-86197-876-9 => " + result + "<br />"); result = IsISBN("ISBN 1 86197 271 7", 10); document.write("ISBN 1 86197 271 7 => " + result + "<br />"); result = IsISBN("ISBN 1 86197 271 6", 10); document.write("ISBN 1 86197 271 6 => " + result + "<br />"); Compiled to JavaScript: expandcollapse popupfunction IsISBN(isbn, type) { if (isbn.length == 0) { return false; } var type10 = 10, type13 = 13; if (type != type10 && type != type13) { return false; } var length = isbn.length - 1; var nine = 57, zero = 48; var char, counter = 0, sum = 0; switch (type) { case type10: { char = isbn[length].charCodeAt(0); if (char == 88 || char == 120) { length--; sum = 10; } counter = 10; for (var i = 0; i <= length; i++) { char = isbn[i].charCodeAt(0); if (char < zero || char > nine) { continue; } sum += (char - zero) * counter; counter -= 1; } return sum % 11 == 0; } case type13: { var one = 1, three = 3; counter = one; for (var i = 0; i <= length; i++) { char = isbn[i].charCodeAt(0); if (char < zero || char > nine) { continue; } sum += (char - zero) * counter; counter = (counter == one) ? three : one; } return sum % 10 == 0; } } return false; } var result = IsISBN("ISBN-13: 978-1-86197-876-9", 13); document.write("ISBN-13: 978-1-86197-876-9 => " + result + "<br />"); result = IsISBN("ISBN 1 86197 271 7", 10); document.write("ISBN 1 86197 271 7 => " + result + "<br />"); result = IsISBN("ISBN 1 86197 271 6", 10); document.write("ISBN 1 86197 271 6 => " + result + "<br />"); Online editor: http://www.typescriptlang.org/Playground/ Edited January 11, 2015 by guinness UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
MadDogVachon Posted March 4, 2015 Share Posted March 4, 2015 (edited) Hi, Here's my function to convert ISBN 10 to ISBN 13 and ISBN 13 to ISBN 10. Thanks to guinness for his code which help me write this one! It may be better written, but that's enough for my needs. expandcollapse popupExample2() Func Example2() Local Const $iType10 = 10, $iType13 = 13 ; 10-digit ISBN numbers. ConvertISBN('ISBN0-9752298-0-4', $iType10) ; Is not a 10-digit ISBN number. ConvertISBN('ISBN1-84356-028-3', $iType10) ; Is a 10-digit ISBN number. ConvertISBN('ISBN0-19-852663-6', $iType10) ; Is a 10-digit ISBN number. ConvertISBN('ISBN 1 86197 271 7', $iType10) ; Is a 10-digit ISBN number. ConvertISBN('ISBN 4 86197 271 7', $iType10) ; Is not a 10-digit ISBN number. ConsoleWrite(@CRLF) ; Empty line. ; 13-digit ISBN numbers. ConvertISBN('ISBN-13: 978-0-306-40615-7', $iType13) ; Is a 13-digit ISBN number. ConvertISBN('ISBN-13: 978-1-86197-876-9', $iType13) ; Is a 13-digit ISBN number. ConvertISBN('ISBN-13: 978-1-86197-876-8', $iType13) ; Is not a 13-digit ISBN number. EndFunc ;==>Example2 Func ConvertISBN($sISBN, $iType) ConsoleWrite($sISBN & ' => ' & ISBNConverter($sISBN, $iType) & @CRLF) EndFunc ;==>ConvertISBN ; #FUNCTION# ==================================================================================================================== ; Name ..........: ISBNConverter ; Description ...: Convert ISBN 10 to ISBN 13 or ISBN 13 to ISBN 10 ; Syntax ........: ISBNConverter($sISBN, $iType) ; Parameters ....: $sISBN - A string value containing an ISBN number. ; $iType - An integer value or either 10 or 13, depending on the ISBN type to convert. ; Return values .: Success: ISBN converted ; Failure: Return the following message: "Wrong ISBN to convert" ; Author ........: Mad Dog Vachon ; Example .......: Yes ; =============================================================================================================================== Func ISBNConverter($sISBN, $iType) If IsISBN($sISBN, $iType) = False Then Return "Wrong ISBN to convert" EndIf Local Const $iType10 = 10, $iType13 = 13 $iType = Int($iType) Local $aArray = StringToASCIIArray($sISBN) Local $iLength = UBound($aArray) - 1 Local Const $iNine = 57, $iEight = 56, $iSeven = 55, $iZero = 48, $iHyphen = 45, $iOne = 1, $iThree = 3 Local $iCounter = 0, $iSum = 0 Switch $iType Case $iType10 Local $sNewISBN = "978-" $iCounter = 3 $iSum = 38 For $i = 0 To $iLength -1 If ($aArray[$i] >= $iZero And $aArray[$i] <= $iNine) Or $aArray[$i] = $iHyphen Then $sNewISBN &= Chr($aArray[$i]) EndIf If $aArray[$i] < $iZero Or $aArray[$i] > $iNine Then ContinueLoop EndIf $iSum += ($aArray[$i] - $iZero) * $iCounter $iCounter = ($iCounter = $iOne) ? $iThree : $iOne Next $sNewISBN &= Mod(10 - Mod($iSum, 10), 10) Return $sNewISBN Case $iType13 Local $sNewISBN = "" Local $b978_9 = False, $b978_7 = False, $b978_8 = False, $b978_Hyphen = False $iCounter = 10 For $i = 0 To $iLength -1 If $b978_Hyphen = False Then If $b978_9 = False Then If $aArray[$i] = $iNine Then $b978_9 = True EndIf ElseIf $b978_7 = False Then If $aArray[$i] = $iSeven Then $b978_7 = True EndIf ElseIf $b978_8 = False Then If $aArray[$i] = $iEight Then $b978_8 = True EndIf ElseIf $b978_Hyphen = False Then If $aArray[$i] = $iHyphen Then $b978_Hyphen = True EndIf EndIf ContinueLoop EndIf If ($aArray[$i] >= $iZero And $aArray[$i] <= $iNine) Or $aArray[$i] = $iHyphen Then $sNewISBN &= Chr($aArray[$i]) EndIf If $aArray[$i] < $iZero Or $aArray[$i] > $iNine Then ContinueLoop EndIf $iSum += ($aArray[$i] - $iZero) * $iCounter $iCounter -= 1 Next Local $iModulus = Mod(11 - Mod($iSum, 11), 11) $sNewISBN &= ($iModulus < 10) ? $iModulus : "X" Return $sNewISBN EndSwitch Return False EndFunc ;==>ISBNConverter ; #FUNCTION# ==================================================================================================================== ; Name ..........: IsISBN ; Description ...: Check if a string contains a valid ISBN number. ; Syntax ........: IsISBN($sISBN, $iType) ; Parameters ....: $sISBN - A string value containing an ISBN number. ; $iType - An integer value or either 10 or 13, depending on the ISBN type to check. ; Return values .: Success: True ; Failure: False and sets @error to non-zero on error: ; 1 = String was empty or contained only whitespace. ; 2 = Type was incorrect. ; 3 = StringToASCIIArray() was empty. ; Author ........: guinness ; Modification ..: Mad Dog Vachon ; Link ..........: https://en.wikipedia.org/wiki/International_Standard_Book_Number ; Example .......: Yes ; =============================================================================================================================== Func IsISBN($sISBN, $iType) If Not StringStripWS($sISBN, 8) Then Return SetError(1, 0, False) Local Const $iType10 = 10, $iType13 = 13 $iType = Int($iType) If $iType <> $iType10 And $iType <> $iType13 Then Return SetError(2, 0, False) Local $aArray = StringToASCIIArray($sISBN) Local $iLength = UBound($aArray) - 1 If $iLength < 0 Then Return SetError(3, 0, False) Local Const $iNine = 57, $iEight = 56, $iSeven = 55, $iZero = 48, $iHyphen = 45, $iOne = 1, $iThree = 3 Local $iCounter = 0, $iNumberDigits = 0, $iSum = 0 Switch $iType Case $iType10 If $aArray[$iLength] = 88 Or $aArray[$iLength] = 120 Then $iLength -= 1 $iNumberDigits += 1 $iSum = 10 EndIf $iCounter = 10 For $i = 0 To $iLength If $aArray[$i] < $iZero Or $aArray[$i] > $iNine Then ContinueLoop EndIf $iNumberDigits += 1 $iSum += ($aArray[$i] - $iZero) * $iCounter $iCounter -= 1 Next If $iNumberDigits <> 10 Then Return False Return Mod($iSum, 11) = 0 ; Divisible by 11. Case $iType13 Local $b978_9 = False, $b978_7 = False, $b978_8 = False, $b978_Hyphen = False $iCounter = $iThree $iSum = 38 $iNumberDigits = 3 For $i = 0 To $iLength If $b978_Hyphen = False Then If $b978_9 = False Then If $aArray[$i] = $iNine Then $b978_9 = True EndIf ElseIf $b978_7 = False Then If $aArray[$i] = $iSeven Then $b978_7 = True EndIf ElseIf $b978_8 = False Then If $aArray[$i] = $iEight Then $b978_8 = True EndIf ElseIf $b978_Hyphen = False Then If $aArray[$i] = $iHyphen Then $b978_Hyphen = True EndIf EndIf ContinueLoop EndIf If $aArray[$i] < $iZero Or $aArray[$i] > $iNine Then ContinueLoop EndIf $iNumberDigits += 1 $iSum += ($aArray[$i] - $iZero) * $iCounter $iCounter = ($iCounter = $iOne) ? $iThree : $iOne Next If $iNumberDigits <> 13 Then Return False Return Mod($iSum, 10) = 0 ; Divisible by 10. EndSwitch Return False EndFunc ;==>IsISBN MDV Edited October 10, 2016 by MadDogVachon Correction of some problems with with conversion and non-valid ISBN 10 and 13... Mad Dog (Maurice) Vachon, a great Canadian professional wrestler! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now