Skip to content

Instantly share code, notes, and snippets.

@masonhale
Forked from chbonser/hyv_checksum.rb
Last active August 4, 2020 13:24
Show Gist options
  • Save masonhale/fa415c6c7fab6679fbf0d1feb5c8988d to your computer and use it in GitHub Desktop.
Save masonhale/fa415c6c7fab6679fbf0d1feb5c8988d to your computer and use it in GitHub Desktop.
Ruby method to generate a .hyv header row checksum
#### HYV EXAMPLE HEADER ROWS
lines = [
"Champs Final 2010;06/27/2010;06/27/2010;05/01/2010;Y;UT Swim Center;;Hy-Tek Sports Software;2.0Hm;CN;7116a",
"Time Trials Summer 2012;05/19/2012;05/19/2012;06/15/2012;Y;YWSC;;Hy-Tek Sports Software;4.0Cf;CN;9111m",
# hyv_checksum generages 0141a for this case (the rest work)
"Peakhurst Junior Sprint Meet 2011;05/22/2011;05/22/2011;05/22/2011;S;Peakhurst Pool;;Hy-Tek Sports Software;3.0Cj;CN;1141a",
"2011 Indian Ocean All Star Challenge;04/23/2011;04/24/2011;04/23/2011;L;Challenge Stadium;;Hy-Tek Sports Software;3.0Dv;CN;21461",
"2009 JSL CHAMPIONSHIP;07/30/2010;07/31/2010;06/01/2010;Y;UVA AQUATICS CENTER;;Hy-Tek Sports Software;2.0Hm;CN;61180",
]
# hyv_checksum generates a header row checksum for a .hyv file.
# The algorithm contained is very close to correct, but not quite
# as test case 3 above has one incorrect 1 character.
def hyv_checksum(values)
# Compute the sum of the ascii integer code for each character
ord_sum = values.reduce(0) { |sum, v| sum + v.ord }
# magic1 because who knows why we do these things
magic1 = ((ord_sum.to_f - 1) / 7).round(half: :even) + 205
# Turn magic1 into more magic... a string of 4 characters, prepending blanks if needed
magic2 = '%4.4s' % magic1.to_s
# Sample some characters out of magic2 and the original string to achieve even more magic and victory
magic2[3] + magic2.slice(0, 3) + values[2]
end
lines.each do |line|
# get the raw values
raw_values = line.split(";")
# pop off the checksum and save it for later comparison
checksum = raw_values.pop
# compute the checksum
computed = hyv_checksum(raw_values.join.split(//))
puts "raw string: '#{raw_values.join}'"
puts "Original Checksum: '#{checksum}'"
puts "Computed Checksum: '#{computed}'"
puts checksum == computed ? "✓ correct" : "✖ wrong"
puts "\n------\n\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment