1

Hi,
I need to create a function to shorten a numerical ID to a string of letters
E.G.

echo encode_num('1'); //Outputs a
echo encode_num('3'); //Outputs c
echo encode_num('26'); //Outputs z
echo encode_num('27'); //Outputs A
echo encode_num('52'); //Outputs Z
echo encode_num('53'); //Outputs aa
echo encode_num('54'); //Outputs ab

etc.

But I also need to be able to decode them again like

echo decode_num('a'); //Outputs 1

Is this possible to do?
Thanx,
Sam Rudge

Edited by samarudge: n/a

4
Contributors
5
Replies
8
Views
7 Years
Discussion Span
Last Post by samarudge
-2

My suggestion:

<?php
function encode_num($number) {
switch($number) {

case 1 :
return "a";
break;

case 2 :
return "b";
break;

case 3 :
return "c";
break;

// ... Alot of code...
// ...Here comes all the other options...
// ...Alot of code...

default:
return "Invalid number";
break;
}
}

function decode_num($string) {
switch($string) {

case "a" :
return 1;
break;

case "b" :
return 2;
break;

case "c" :
return 3;
break;

// ... Alot of code...
// ...Here comes all the other options...
// ...Alot of code...

default:
return "Invalid string";
break;
}
}
echo "The letter 'c' decoded: ".decode_num('c')."<br />";
echo "The number '3' encoded: ".encode_num('3')."<br />";
?>

~G

0

My suggestion:

<?php
function encode_num($number) {
switch($number) {

case 1 :
return "a";
break;

case 2 :
return "b";
break;

case 3 :
return "c";
break;

// ... Alot of code...
// ...Here comes all the other options...
// ...Alot of code...

default:
return "Invalid number";
break;
}
}

function decode_num($string) {
switch($string) {

case "a" :
return 1;
break;

case "b" :
return 2;
break;

case "c" :
return 3;
break;

// ... Alot of code...
// ...Here comes all the other options...
// ...Alot of code...

default:
return "Invalid string";
break;
}
}
echo "The letter 'c' decoded: ".decode_num('c')."<br />";
echo "The number '3' encoded: ".encode_num('3')."<br />";
?>

~G

You have the right idea but I need it to convert an possibly infinite number to a string like aYnqW

1

Here you go. I'll let you do the decode part

<?php
for($i=1;$i<100;$i++)
{
        echo $i. " ".convertNumtoLetter($i)."\n";
}
function convertNumtoLetter($in)
{
        $myarray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $mod = $in%52;
        $outputStr = '';
        if($in == 52)
        {
                return $myarray[$in-1];
        }
        if($in-$mod==0)
        {
                $outputStr = $myarray[$mod-1];
        }
        else
        {
                $outputStr = convertNumtoLetter(($in-$mod)/52) . $myarray[$mod-1];
        }
        return ($outputStr);
}
?>
0

I prefer this approach.

<?php

for($i=1;$i<200;$i++)
{
    echo $i. " ".convertNumtoLetter($i)."\n";
}
for($i=1;$i<200;$i++)
{
    echo $i. " ".convertNumtoLetter($i)." ". convertLettertoNum(convertNumtoLetter($i))."\n";
}
function convertNumtoLetter($in)
{
    $mod = $in%52;
    $outputStr = '';
    if($in == 52)
    {
        return chr(asciiOffset($in));
    }
    if($in-$mod==0)
    {
        $outputStr = chr(asciiOffset($in));
    }
    else
    {
        if ($mod == 0)
        {
            $outputStr = convertNumtoLetter(($in-$mod)/52 - 1) . chr(asciiOffset(52));
        } else {
            $outputStr = convertNumtoLetter(($in-$mod)/52) . chr(asciiOffset($mod));
        }
    }
    return ($outputStr);
}
function convertLettertoNum($in)
{
    $len = strlen($in);
    for ($i = $len - 1; $i >= 0; $i--)
    {
        $val += pow(52,$i) * asciiReverse(ord(substr($in,-($i+1),1)));
    }
    return $val;
}
function asciiReverse($int)
{
    if ($int < 96)
    {
        return $int - 38;
    } else {
        return $int - 96;
    }
}
function asciiOffset($int)
{
    if ($int < 27)
    {
        return $int + 96;
    } else {
        return $int + 38;
    }
}

?>
0

OK it was a whole lot simpler than I though:
All I have to do is convert to Base32 so my functions are

function id_encode($ID) {
	$Hash = base_convert($ID, 10, 32);
	return $Hash;
}
function id_decode($ID) {
	$Hash = base_convert($ID, 32, 10);
	return $Hash;
}

but thanx for your help =)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.