Is there any format we can use to identify UPC code instead of checking only for number of digits ?
Asked
Active
Viewed 2,187 times
5
-
UPCs [include checksum digits](http://en.wikipedia.org/wiki/Universal_Product_Code#Check_digits), so you could determine whether a given 12-digit number is a valid UPC or not. Depending upon your application, you might be able to use some of the other information on that Wikipedia page to decide whether a given number is the kind of UPC you're interested in. – bdesham Jun 21 '13 at 14:13
-
1this really is not a iOS or Objective-C specific question but there is no "upc" tag in StackOverflow for me to edit your question to. [There is some C# code in this question that looks very easily portable to what you are trying to do](http://stackoverflow.com/questions/10143547/how-do-i-validate-a-upc-or-ean-code). – Michael Dautermann Jun 21 '13 at 14:13
2 Answers
9
I have some Objective-C code that validates a user entered string to see if it is a valid UPC or EAN barcode. This supports UPC, ISBN, and EAN (8, 13, and 14).
If you have a number, convert it to a string first to use this method. This method assumes that the barcode string only has digits 0-9 or an X (some ISBN barcodes can have an X).
- (BOOL)validBarcode:(NSString *)code {
int len = [code length];
switch (len) {
case 8: // EAN-8
{
int check = [code intForDigitAt:7];
int val = (10 -
(([code intForDigitAt:1] + [code intForDigitAt:3] + [code intForDigitAt:5] +
([code intForDigitAt:0] + [code intForDigitAt:2] + [code intForDigitAt:4] + [code intForDigitAt:6]) *
3) % 10)) % 10;
return check == val;
}
case 10: // ISBN
{
int check = [code intForDigitAt:9];
int sum = 0;
for (int i = 0; i < 9; i++) {
sum += [code intForDigitAt:i] * (i + 1);
}
int val = sum % 11;
if (val == 10) {
return [code characterAtIndex:9] == 'X' || [code characterAtIndex:9] == 'x';
} else {
return check == val;
}
}
case 12: // UPC
{
int check = [code intForDigitAt:11];
int val = (10 -
(([code intForDigitAt:1] + [code intForDigitAt:3] + [code intForDigitAt:5] + [code intForDigitAt:7] + [code intForDigitAt:9] +
([code intForDigitAt:0] + [code intForDigitAt:2] + [code intForDigitAt:4] + [code intForDigitAt:6] + [code intForDigitAt:8] + [code intForDigitAt:10]) *
3) % 10)) % 10;
return check == val;
}
case 13: // EAN-13
{
int check = [code intForDigitAt:12];
int val = (10 -
(([code intForDigitAt:0] + [code intForDigitAt:2] + [code intForDigitAt:4] + [code intForDigitAt:6] + [code intForDigitAt:8] + [code intForDigitAt:10] +
([code intForDigitAt:1] + [code intForDigitAt:3] + [code intForDigitAt:5] + [code intForDigitAt:7] + [code intForDigitAt:9] + [code intForDigitAt:11]) *
3) % 10)) % 10;
return check == val;
}
case 14: // EAN-14
{
int check = [code intForDigitAt:13];
int val = (10 -
(([code intForDigitAt:1] + [code intForDigitAt:3] + [code intForDigitAt:5] + [code intForDigitAt:7] + [code intForDigitAt:9] + [code intForDigitAt:11] +
([code intForDigitAt:0] + [code intForDigitAt:2] + [code intForDigitAt:4] + [code intForDigitAt:6] + [code intForDigitAt:8] + [code intForDigitAt:10] + [code intForDigitAt:12]) *
3) % 10)) % 10;
return check == val;
}
default:
return NO;
}
}
This makes use of a category method I added to NSString
:
- (int)intForDigitAt:(NSUInteger)index {
unichar ch = [self characterAtIndex:index];
if (ch >= '0' && ch <= '9') {
return ch - '0';
} else
return 0;
}

rmaddy
- 314,917
- 42
- 532
- 579
1
There's no objective-c format or way of doing this, you'll have to implement yourself the function that will:
a) Check the number of digits b) Check the checksum digit

Antonio MG
- 20,382
- 3
- 43
- 62