Skip to content

Instantly share code, notes, and snippets.

@cxa
Created October 17, 2010 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cxa/630508 to your computer and use it in GitHub Desktop.
Save cxa/630508 to your computer and use it in GitHub Desktop.
- (NSString *)convertISBN10To13:(NSString *)isbn10
{
NSString *prefix = [NSString stringWithFormat:@"978%@", [isbn10 substringToIndex:9]];
NSInteger sum = 0;
for (int i=0; i<12; i++){
char c = [prefix characterAtIndex:i];
int n = c - '0';
int w = (i % 2) ? 3 : 1;
sum += w * n;
}
int check = 10 - (sum % 10);
if (check == 10) check = 0;
return [NSString stringWithFormat:@"%@%d", prefix, check];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment