Reply to Re: Auto-increment numbers and letters possible?

Your name:

Reply:


Posted by Hilarion on 06/24/05 17:41

Justin Sane wrote:

> > I'd like to start from 1 to ZZZZZZZ. That would combine 10 numbers + 26
> > lower-case letters + 26 upper-case letters.

Andy Hassall wrote:

> Use base_convert to convert to base36.

I suppose Justin meant base 62 (treat 'a' and 'A' differently) and base_convert
does not do it (it treats 'a' and 'A' same, giving only 36 unique digits).

Something like the code below should work for conversion between INT and base
62 (but there are faster and better ways). The problem is that ZZZZZZZ
represents 3521614606207 which is greater than max 32 bit signed int value
(2147483647). With this you could reach only number 2lkCB1 (in base 62).

<?php
error_reporting( E_ALL );

$b62 = array(
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'
);
$rev_b62 = array(
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
'a' => 10, 'b' => 11, 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17, 'i' => 18, 'j' => 19,
'k' => 20, 'l' => 21, 'm' => 22, 'n' => 23, 'o' => 24, 'p' => 25, 'q' => 26, 'r' => 27, 's' => 28, 't' => 29,
'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33, 'y' => 34, 'z' => 35,
'A' => 36, 'B' => 37, 'C' => 38, 'D' => 39, 'E' => 40, 'F' => 41, 'G' => 42, 'H' => 43, 'I' => 44, 'J' => 45,
'K' => 46, 'L' => 47, 'M' => 48, 'N' => 49, 'O' => 50, 'P' => 51, 'Q' => 52, 'R' => 53, 'S' => 54, 'T' => 55,
'U' => 56, 'V' => 57, 'W' => 58, 'X' => 59, 'Y' => 60, 'Z' => 61
);

function b10to62( $i )
{
global $b62;
$i = IntVal( $i );
if ($i==0)
return '0';
if ($i < 0)
{
$sgn = '-';
$i = -$i;
}
else
$sgn = '';
$result = '';
while ($i)
{
$result = $b62[ $i % 62 ] . $result;
$i = IntVal( $i / 62 );
}
return $sgn . $result;
}

function b62to10( $s )
{
global $rev_b62;
$s = trim( $s );
$result = 0;
if ($s{0}=='-')
{
$sgn = -1;
$s = substr( $s, 1 );
}
else
$sgn = 1;
$len = strlen( $s );
for( $i=0; $i < $len; $i++ )
{
if (!isset( $rev_b62[$s{$i}] ))
{
trigger_error( 'Unknown char: ' . $s{$i}, E_USER_ERROR );
return NULL;
}
$result *= 62;
$result += $rev_b62[$s{$i}];
}
return $sgn * $result;
}

set_time_limit( 0 );
for( $i = 0; $i <= 2147483647; $i++ )
{
printf( "%11d:%9s:%11d\n", $i, b10to62( $i ), b62to10( b10to62( $i ) ) );
flush();
}

?>





To do incrementation you do not need conversion.

<?php

$b62 = array(
// as above
);
$rev_b62 = array(

// as above
);

function increment( $b62num, $incr_by_int )
{
global $b62, $rev_b62;
$b62num = trim( $b62num );
if ($b62num == '')
{
trigger_error( 'Wrong parameter.', E_USER_ERROR );
return NULL;
}
$last_digit = $rev_b62[ substr( $b62num, -1, 1 ) ] + IntVal( $incr_by_int );
$incr_by_int = IntVal( $last_digit / 62 );
$last_digit = $b62[ $last_digit % 62 ];
$b62num = substr( $b62num, 0, -1 );
if (!$incr_by_int)
return $b62num . $last_digit;
if ($b62num == '')
$b62num = '0';
return increment( $b62num, $incr_by_int ) . $last_digit;
}

$txt = '00000';
$step = 63;
while (strlen($txt) < 8)
{
echo $txt . "\n";
$txt = increment( $txt, $step );
}

?>

The "b62num" param can't be less than '0' but can be as long
(as big) as you want. The "incr_by_int" should be between zero
and (MAX_INT - 61).
It has one feature: it keeps leading zeros.
It should be easy to adapt it to support decrementing
($incr_by_int < 0).


Hilarion

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация