Computing VIN checksums
I've had to work a little with VIN numbers lately, and so I looked back at a post I wrote on the subject three years ago. That post goes into the details of Vehicle Identification Numbers and the quirky algorithm used to compute the check sum.
This post captures the algorithm in Python code. See the earlier post for documentation.
import re def char_to_num(ch): "Assumes all characters are digits or capital letters." n = ord(ch) if n
This code assumes the VIN number is given as ASCII or Unicode text. In particular, digits come before letters, and the numeric values of letters increase with alphabetical order.
The code could seem circular: the input is the full VIN, including the checksum. But the checksum goes in the 9th position, which has weight 0. So the checksum doesn't contribute to its own calculation.
Update I added a regular expression to check that the VIN contains only valid characters.
The post Computing VIN checksums first appeared on John D. Cook.