Skip to content

Instantly share code, notes, and snippets.

@MrBogomips
Last active October 29, 2021 17:25
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 MrBogomips/8715832d1f868c5e74bab8e2b3305205 to your computer and use it in GitHub Desktop.
Save MrBogomips/8715832d1f868c5e74bab8e2b3305205 to your computer and use it in GitHub Desktop.
static readonly byte[] _oddMap = new byte[] { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 };
static bool PartitaIvaCheckSum(string value)
{
Debug.Assert(value is not null);
Debug.Assert(value.Length == 11);
// ASSERT: value is a string of digits
var c0 = (byte) '0';
int acc = 0;
byte[] codes = Encoding.ASCII.GetBytes(value);
for (var pos = 0; pos < 11; ++pos)
{
var ci = codes[pos] - c0;
if (pos % 2 == 0) acc += ci;
else acc += _oddMap[ci];
}
return acc % 10 == 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment