Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1 digit checksum algorithm

Status
Not open for further replies.

ganj

IS-IT--Management
Nov 3, 2001
19
AT
I am doing a POS application. To acquire data, I am using a barcode scanner.

I wanted to add a checksum to the code read by the scanner. The checksum should be 1 digit.

What I have in mind is something like the ISBN number for books, where the last digit is also a checksum value. Any ideas on algorithms how to calculate a checksum from a serious of numerical values?

thank you,
 
You could use the modulo 10 algorithm used to validate credit card numbers or SSN.

Here is the code of a method I use to validate credit card numbers. Check out the Modulo 10 part.


Code:
LOCAL lcCredNoString, lcSpacers, ln_i
LOCAL ln_Mod10, ln_pos, ln_digit

IF EMPTY(THIS.value)
	THIS.lValidCard = .F.
	THIS.cCardType	= ''
	RETURN
ENDIF
	
** Copy this value to a working variable
lcCredNoString = ALLTRIM(THIS.Value)

** Remove some caracters used as spacers
lcSpacers = ' -./*\'
FOR ln_i = 1 TO LEN(m.lcSpacers)
	lcCredNoString = STRTRAN(lcCredNoString,  ;
		SUBSTR(m.lcSpacers, m.ln_i, 1) ,'')
ENDFOR

** Get all digits except the last one
lcCredNoString = SUBSTR(lcCredNoString, 1, LEN(lcCredNoString) - 1 )

** Calculate the modulus 10 for lcCreditNoString
m.ln_mod10 = 0
FOR m.ln_pos = 1 TO LEN(lcCredNoString)
	m.ln_Digit =VAL( SUBSTR(lcCredNoString, m.ln_pos , 1) )	
	** If in odd numbered position, multiply by two
	m.ln_digit = m.ln_digit * IIF(m.ln_pos % 2 = 0, 1, 2)

	** If the result is greater than 10, add the two digits together
	m.ln_digit = INT(m.ln_digit / 10)  + m.ln_digit % 10
	
	** Additonner les nombres ensemble
	m.ln_mod10 = m.ln_mod10 + m.ln_digit
ENDFOR

m.ln_mod10 = ALLTRIM(STR(10 - (m.ln_mod10 % 10)))
m.ln_mod10 =  IIF(m.ln_mod10 = '10', '0', m.ln_mod10)

** Prepare the return value
THIS.lValidCard = (RIGHT(ALLTRIM(THIS.value),1) = m.ln_mod10)
** Type of card
DO CASE
	CASE LEFT(THIS.value,1) = '5'
		THIS.cCardtype = "MasterCard"
	CASE LEFT(THIS.value,1) = '4'
		THIS.cCardtype = "Visa"	
	CASE LEFT(THIS.value,1) = '3'
		THIS.cCardtype = "American Express"
	OTHERWISE
		THIS.cCardtype = "U"
ENDCASE
RETURN THIS.lValidCard

Good luck

Jean
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top