Created
September 17, 2011 15:00
-
-
Save eijik/1224014 to your computer and use it in GitHub Desktop.
26hexiadecimal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Number <-> AZ | |
# 26 hexiadecimal AZ -> Number | |
# ex. 'AA'.to_i26 => 27 | |
class String | |
def to_i26 | |
return nil unless self.to_s =~ /^[a-zA-Z]+$/ | |
self.upcase! | |
str = "" | |
n = 1 | |
#each char | |
self.bytes do |s| | |
#string initial need adjust | |
aj = (self.to_s.size > 1 && n == 1)? 1 : 0 | |
if s <= 74 | |
#A-J => 0-9 | |
str << (s - 65 + aj).to_s | |
else | |
#K-Z => A-Q | |
str << (s - 10 + aj).chr | |
end | |
n += 1 | |
end | |
str.to_i(26) + 1 | |
end | |
end | |
# 26 hexiadecimal Number -> AZ | |
# ex. 28.to_i26 => 'AA' | |
class Integer | |
def to_s26 | |
str=(self-1).to_s(26).upcase | |
n=1 | |
re="" | |
str.bytes do |s| | |
#string initial need adjust | |
aj = (str.size > 1 && n == 1)? 1 : 0 | |
if s <= 57 | |
#0-9 => A-J | |
re << (s + 17 - aj).chr | |
else | |
#A-Q => K-Z | |
re << (s + 10 - aj).chr | |
end | |
n += 1 | |
end | |
re | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment