The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 20, 2019, 11:38:29 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Int to Alpha function  (Read 2081 times)
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« on: September 04, 2009, 09:24:54 PM »

For my next evil plan needs...

Is there a function that will convert an integer to an alphanumeric string.
IE compress the string?
It also needs to be bi-directional.

something like 70257924=a7n3

thoughts?

I was thinking that I could always divide the number by 26 each iteration until I have a remainder less than 26?
or actually 36 for a-z0-9.
or even 62 for a-zA-z0-9

hmmm i think i just solved it lol.
that would mean that every 62nd integer value would be Z, assuming Z was the last char value in my conversion string.

make sense?
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
kurdt
Lifer
*****
Offline Offline

Posts: 1153


paha arkkitehti


View Profile
« Reply #1 on: September 04, 2009, 11:13:05 PM »

gzcompress? Cheesy
Logged

I met god and he had nothing to say to me.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #2 on: September 05, 2009, 09:22:38 PM »

unfortunately that results in extended characters.

Basically I want to be able to compress a number like 750907234928747091
into something like z7R9q
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #3 on: September 05, 2009, 09:41:09 PM »

Though you got me looking online for a different solution. here it is (poached a bit from here: http://snook.ca/archives/php/url-shortener)

Perfect! plus you can add your own char choices by defining the codeset. you just need more characters than your longest number, technically.
But in this case, i could add .-_+ among a whole boatload of others. woot.

<?php
$codeset 
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

$val CompressNumber('79379345',$codeset);
echo 
"$val<br>";
echo 
DecompressNumber($val,$codeset);

function 
CompressNumber($number,$codeset)
{
	
$base strlen($codeset);
	
$compressed '';
	
while (
$number 0) {
	
  
$compressed substr($codeset, ($number $base), 1) . $compressed;
	
  
$number floor($number/$base);
	
}
	
return 
$compressed;
}

function 
DecompressNumber($compressed,$codeset)
{
	
$base strlen($codeset);
	
$number 0;
	
for (
$i strlen($compressed); $i$i--)
	
{
	
  
$number += strpos($codesetsubstr($compressed, (-* ( $i strlen($compressed) )),1)) * pow($base,$i-1);
	
}
	
return 
$number;
}
?>
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #4 on: September 08, 2009, 09:54:46 AM »

Sorry just got back here nuts... that's very similar to my technique. I use base35, which is alpha & numeric chars, only because I din't want any -_+() etc in my codes. Also, you can go another 26 further if you can use case sensitivity, but since I use these codes as a PK in MySQL databases (which are all insensitive) then it doesn't work.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #5 on: September 08, 2009, 10:42:43 AM »

Yea. These codes will be translated to INT which is the PK in the db. So I am only doing int lookups Wink
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!