Calculation of a modulo 10 control key

Just starting out? Need help? Post your questions and find answers here.
loulou2522
Enthusiast
Enthusiast
Posts: 552
Joined: Tue Oct 14, 2014 12:09 pm

Calculation of a modulo 10 control key

Post by loulou2522 »

I am looking for an example that allows the calculation of a key from a modulo 10 example.
210000000003139471430009017
the result is 7 (position 27 of the figure)
Thank you in advance for helping me
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Calculation of a modulo 10 control key

Post by wilbert »

What type of numbers do you have ?
Internal formats PB supports like Quad or Double aren't suitable for this kind of large integer values.
If they are strings, you can just take the last character.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
NicTheQuick
Addict
Addict
Posts: 1523
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Calculation of a modulo 10 control key

Post by NicTheQuick »

If your numbers are already represented as decimal strings then just extract the last digit and there you go.

In all other cases: Please give us more information.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
BarryG
Addict
Addict
Posts: 4178
Joined: Thu Apr 18, 2019 8:17 am

Re: Calculation of a modulo 10 control key

Post by BarryG »

NicTheQuick wrote:just extract the last digit
Surely that's not always going to be the case? We're talking key calculations here. The 7 in the original post was just an example for that specific calculation, no?
User avatar
NicTheQuick
Addict
Addict
Posts: 1523
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Calculation of a modulo 10 control key

Post by NicTheQuick »

What is a key calculation? Are we talking about private and public keys or signatures? I don't get it.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 676
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Calculation of a modulo 10 control key

Post by Kurzer »

Nic, I suspect that he means a so called "check digit calculation" (in german "Prüfziffernberechnung").

@loulou2522:

This is a modulo 10 checksum calculation with weighting 2.

Code: Select all

Procedure.s CalcChecksum(sNumber.s)
	Protected.i i, iDigit, iSum, iLen
	
	iLen = Len(sNumber)
		
		iSum = 0
		For i = 1 To iLen
			iDigit = Val(Mid(sNumber, i, 1))
			If i % 2 = 0 : iDigit * 2 : EndIf
			iSum + iDigit
		Next i
		ProcedureReturn Str((10 - iSum % 10) %10)
EndProcedure

Debug CalcChecksum("21000000000313947143000901")
You can get information about the check digit calculation methods here:
https://www.activebarcode.com/codes/che ... ulo10.html

Markus
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
Mohawk70
Enthusiast
Enthusiast
Posts: 404
Joined: Thu May 11, 2006 1:04 am
Location: Florida, USA

Re: Calculation of a modulo 10 control key

Post by Mohawk70 »

kurzer wrote:Nic, I suspect that he means a so called "check digit calculation" (in german "Prüfziffernberechnung").

@loulou2522:

This is a modulo 10 checksum calculation with weighting 2.

Code: Select all

Procedure.s CalcChecksum(sNumber.s)
	Protected.i i, iDigit, iSum, iLen
	
	iLen = Len(sNumber)
		
		iSum = 0
		For i = 1 To iLen
			iDigit = Val(Mid(sNumber, i, 1))
			If i % 2 = 0 : iDigit * 2 : EndIf
			iSum + iDigit
		Next i
		ProcedureReturn Str((10 - iSum % 10) %10)
EndProcedure

Debug CalcChecksum("21000000000313947143000901")
You can get information about the check digit calculation methods here:
https://www.activebarcode.com/codes/che ... ulo10.html

Markus
Google "Luhn Formula" or "Luhn calculation"
HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
Post Reply