Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
How much shorter are your URLs than the competion’s?
u.nu’s ground-breaking 3-letter domain technology is 25% shorter than its closest imitators, which have 4-letter domains, and it’s fully 70% shorter than arch-rival tinyurl.com’s rambling Dostoyevskian screed of a domain name. Unlike all those other sites, which make no such assurances, we offer a 100% guarantee that our domain name will never get longer. Add those percentages up and you get 195%, which is quite a lot.
long long id = 7391749137491, tmp = 0 , tmp2 = 0 ;(подсвечено с помощью highlight.hohli.com/)
char curr_digit = 0 ;
char alphabet[] = { 'a', ..., 'z', 'A', ..., 'Z', '0', ..., '9' }; // 62 символа
#define ALPH_SIZE 62
#define ALPH_MAXLEN 12 // посчитайте сами log_62(2^64)
char result[ALPH_MAXLEN+1]; // лишний байт на хранение 0 символа
int i = 0 ;
tmp = id;
for(i = 0 ; i < ALPH_MAXLEN; i++)
{
cur_digit %= ALPH_SIZE;
result[i] = cur_digit;
tmp /= ALPH_SIZE;
if(!tmp) break;
}
result[i+1] = 0 ;
// далее нужно перевернуть строку
int l = i; // длину строки мы знаем
char tmpchar = 0 ;
for(i = 0 ; i<l/2; i++)
{
tmpchar = result[l-i];
result[l-i] = result[i];
result[i] = tmpchar;
}
result[i] = cur_digit;
result[i] = alphabet[cur_digit];
cur_digit %= ALPH_SIZE;cur_digit = tmp%ALPH_SIZE;function fbc( $num, $base=62, $index=false ) {
if (! $base ) {
$base = strlen( $index );
} else if (! $index ) {
$index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" , ,$base );
}
$out = "";
for ( $t = floor( log10( $num ) / log10( $base ) ); $t >= ; $t-- ) {
$a = floor( $num / pow( $base, $t ) );
$out = $out . substr( $index, $a, 1 );
$num = $num - ( $a * pow( $base, $t ) );
}
return $out;
}
function cbf( $num, $base=62, $index=false ) {
if (! $base ) {
$base = strlen( $index );
} else if (! $index ) {
$index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", , $base );
}
$out = ;
$len = strlen( $num ) - 1;
for ( $t = ; $t <= $len; $t++ ) {
$out = $out + strpos( $index, substr( $num, $t, 1 ) ) * pow( $base, $len - $t );
}
return $out;
}
Нашел применение домену-трехсимвольнику — сервис коротких URL