Skip to content

Instantly share code, notes, and snippets.

@TonnyXu
Last active August 29, 2015 14:21
Show Gist options
  • Save TonnyXu/7f7f3b15a468811df207 to your computer and use it in GitHub Desktop.
Save TonnyXu/7f7f3b15a468811df207 to your computer and use it in GitHub Desktop.
JAN2EAN.rb
def genFontStr13(str)
if (str =~ /^\d{13}$/).nil? then
return nil
end
table = ["AAAAAA", "AABABB", "AABBAB", "AABBBA", "ABAABB", "ABBAAB", "ABBBAA", "ABABAB", "ABABBA", "ABBABA"]
first = str[0,1]
first6 = str[1,6]
last6 = str[7,6]
table2 = table[first.to_i]
result = first
first6.chars.each_with_index do |char, i|
target = table2[i, 1]
if target == 'A' then
result += (65 + char.to_i).chr
elsif target == 'B' then
result += (75 + char.to_i).chr
end
end
result += "*"
last6.chars.each_with_index do |char, i|
result += (97+char.to_i).chr
end
result += "+"
return result
end
def genFontStr8(str)
if (str =~ /^\d{8}$/).nil? then
return nil
end
first6 = str[0,4]
last6 = str[4,4]
result = ":"
first6.chars.each_with_index do |char, i|
result += (65 + char.to_i).chr
end
result += "*"
last6.chars.each_with_index do |char, i|
result += (97+char.to_i).chr
end
result += "+"
return result
end
t1 = genFontStr13("1234")
t2 = genFontStr13("4569951116179")
t3 = genFontStr8("123")
t4 = genFontStr8("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment