Converting integers into alphabetical letters

For a project, I would like to know how to convert a number into a alphabeticalized letters.
My idea is that I could run a number as a parameter through a function that converts that number into specific letters.

A system like this:

I have absolutely no idea where to begin with this and any help is appreciated.

1 Like

An idea is to take the number and divide it by 26,and find the remainder. If number/26 = 0.something,then you know first alphabet is nil. Not sure how but you have to find out hoe to get the remainder.

1 Like

You’re basically converting base-10 to base-26.

You could use the string library functions to achieve this. You will need to use the numbers on this table, however:

An example code:

local function NumToLetter(Num)
    return string.char(Num + 64) -- Adding 64 so that putting 1 returns "A"
end

local Letter = NumToLetter(1)
print(Letter) -- A
print(string.byte(Letter)) -- 65
4 Likes

Tested and works in my studio:

local alphabet = string.split("BCDEFGHIJKLMNOPQRSTUVWXYZ", "")
alphabet[0] = "A"
local function convert(i)
	if (i == 0) then
		return "A" -- weird edge case too lazy to fix
	end
	local s = ""
	local n = i - 1
	local c
	while (n > 0) do
		c = n % 26
		s = alphabet[c]..s
		n = math.floor(n / 26)
	end
	return s
end

for i = 1, 79 do
	print(i, "\t", convert(i))
end

(edit) I see the error im working on it now…

Explanation and Walkthrough

You can do this; make a table called whatever you’d like. In this case, we’ll name it “conversions.” Next, fill in the table with tables inside of it, with those “nested tables” being formed of two elements. First the number, and second the letter(s) you want that number to be referenced to. Following the picture you showed in your post, it should look like this:

local conversions = {
    {1, "A"},
    {2, "B"},
    {3, "C"},
    {27, "AA"},
    {28, "AB"},
    {29, "AC"},
    {52, "BA"},
    {53, "BB"},
    {54, "BC"}
}

Then make a function that will take input as the number you want to convert, linearly search through the table for that number, and return the according letter it finds in the table. Like this:

local function returnLetter(numInput)
    for i = 1, #conversions, 1 do
        if conversions[i][1] == numInput then
            return conversions[i][2]
        end
    end
end

Then to use this function, all you need to do is set a variable to the function itself, with the parameter being the number you’d like to have converted. Then in this case, we’ll simple print the return value:

local ret = returnLetter(2)
print(ret)

The Final Result

local conversions = {
    {1, "A"},
    {2, "B"},
    {3, "C"},
    {27, "AA"},
    {28, "AB"},
    {29, "AC"},
    {52, "BA"},
    {53, "BB"},
    {54, "BC"}
}

local function returnLetter(numInput)
    for i = 1, #conversions, 1 do
        if conversions[i][1] == numInput then
            return conversions[i][2]
        end
    end
end

local ret = returnLetter(2)
print(ret)

Hopefully this helped! Have a good week.

Extra Note: Don’t worry about changing the values in the table. As long as you follow the same format - the tables with the numbers first and letters second - and make sure to place commas between those tables that are within the main conversions table, everything should work fine and self adjust.

Thank you for your reply. This works just the way I wanted it to. I’ve been experimenting with this for a very long time and couldn’t seem to make any progress. I’m very relieved that you found a solution.

Thanks, that code I posted actually had an issue with offset letters. Here is one that should work (at least up to number 702 ‘ZZ’):

(edit: after more tests it seems to work with numbers higher than 702 :+1:)

local alphabet = string.split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
local function convert(i)
	if (alphabet[i]) then
		return alphabet[i]
	else
		local n = i
		local s = ""
		local r = (n - 1) % 26 + 1
		local d = math.floor(n / 26)
		if (r == 26) then d = d - 1 end -- hacky fix to the offset problem
		return convert(d)..convert(r)
	end
end

for i = 1, 79 do
	print(i, "\t", convert(i))
end