# Auto-increment numbers and letters possible?

• June 23rd, 08:37 PM
Justin
Auto-increment numbers and letters possible?
Is there a way to auto-increment mixed lower and upper case letters, and
numbers?

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

I think it's pretty tricky but there's gotta be a way...

--
Thanks,

Justin.
http://www.opera.com/mail/
• June 23rd, 09:06 PM
Andy
Re: Auto-increment numbers and letters possible?
On Thu, 23 Jun 2005 17:37:39 -0300, "Justin Sane" <net> wrote:

Use base_convert to convert to base36.

<?php
for (\$i=0; \$i<46656; \$i++)
echo base_convert(\$i, 10, 36). "<br>";
?>

--
Andy Hassall / <co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage ysis tool
• June 24th, 09:56 AM
Marcos
Re: Auto-increment numbers and letters possible?
Em Thu, 23 Jun 2005 21:06:09 -0000, Andy Hassall <co.uk>
escreveu:

Very Good...

Sorry, but I am curious. And I'd like to know how this work. I still did
not undertand very well.

--

Thanks,

________________________________________
Usando o M2 do Opera: http://www.opera.com/m2/
• June 24th, 02:41 PM
Hilarion
Re: Auto-increment numbers and letters possible?
Justin Sane wrote:
[/ref]

Andy Hassall wrote:

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
• June 24th, 03:08 PM
Hilarion
Re: Auto-increment numbers and letters possible?
Andy Hassall wrote:
[/ref]

Marcos wrote:

How what works? The function "base_convert" takes a string (first param)
representing a number with given base (in second param) and converts
it to a string representing same number in another base (given in third
param).

executing this | means this
--------------------------------+------------------------------------
base_convert( '1234', 10, 2 ) | convert decimal 1234 to binary
base_convert( '1010', 2, 8 ) | convert binary 1010 to octal
base_convert( '1FEA', 16, 2 ) | convert hexadecimal 1FEA to binary

You can use any base between 2 and 36. The base_convert uses decimal digits
from '0' to '9' and base letters (from 'A' to 'Z' - it does not matter
if letter is capital or not) giving them values from 0 to 9 (for digits)
and from 10 to 35 (for letters). This 36 digit set is truncated for
the specified base, so for binary input / output we have 0 and 1, for
octal - 0, 1, 2, 3, 4, 5, 6, 7, for decimal from 0 to 9, for hexadecimal
- 0..9, A, B, C, D, E, F.

The first parameter is converted to string, so base_convert( 1010, 2, 8 )
gives same result as base_convert( '1010', 2, 8 ).

Hilarion
• June 24th, 03:32 PM
Hilarion
Re: Auto-increment numbers and letters possible?
Hilarion wrote:

The above line in "increment" function should be replaced with lines:

\$last_digit = substr( \$b62num, -1, 1 );
if (!isset(\$rev_b62[ \$last_digit ]))
{
trigger_error( Unknown char: ' . \$last_digit, E_USER_ERROR );
return NULL;
}
\$last_digit = \$rev_b62[ \$last_digit ] + IntVal( \$incr_by_int );

to turn on \$b62num value validation.

Hilarion
• June 24th, 03:38 PM
Hilarion
Re: Auto-increment numbers and letters possible?
Hilarion wrote:

To generate \$rev_b62 array one could use array_flip function,
but using it each time the page is loaded could slow things
down a bit so I used the function result in the code.

Hilarion
• June 24th, 03:45 PM
Hilarion
Re: Auto-increment numbers and letters possible?
Hilarion wrote:

>
> The above line in "increment" function should be replaced with lines:
>
> \$last_digit = substr( \$b62num, -1, 1 );
> if (!isset(\$rev_b62[ \$last_digit ]))
> {
> trigger_error( Unknown char: ' . \$last_digit, E_USER_ERROR );
> return NULL;
> }
> \$last_digit = \$rev_b62[ \$last_digit ] + IntVal( \$incr_by_int );
>
> to turn on \$b62num value validation.[/ref]

But it validates only the digits it's changing so increment( '*&%&000', 36 )
would raise no error.

Hilarion