큰숫자 -> 문자열로 압축 -> 다시 해제 함수모음 / base_convert() Base 62 > 개발자 메모장

개발자 메모장

큰숫자 -> 문자열로 압축 -> 다시 해제 함수모음 / base_convert() Base 62 정보

큰숫자 -> 문자열로 압축 -> 다시 해제 함수모음 / base_convert() Base 62

본문

큰숫자를 대소문자를 포함한 62 base로 변환하여 압축함

틀린그림찾기 소스에 적용함..

 

<?php

 

function int2base($n){
    $r = '';
    for ($i = 1; $n >= 0 && $i < 10; $i++) {
        $r = chr(0x41 + ($n % pow(26, $i) / pow(26, $i - 1))) . $r;
        $n -= pow(26, $i);
    }
    return $r;
}


function base2int($a) {
    $r = 0;
    $l = strlen($a);
    for ($i = 0; $i < $l; $i++) {
        $r += pow(26, $i) * (ord($a[$l - $i - 1]) - 0x40);
    }
    return $r - 1;
}

function base_convert_alt($val,$from_base,$to_base){
    static $gmp;
    static $bc;
    static $gmp62;
    if ($from_base<37) $val=strtoupper($val);
    if ($gmp===null) $gmp=function_exists('gmp_init');
    if ($gmp62===null) $gmp62=version_compare(PHP_VERSION,'5.3.2')>=0;
    if ($gmp && ($gmp62 or ($from_base<37 && $to_base<37)))
    return gmp_strval(gmp_init($val,$from_base),$to_base);
    if ($bc===null) $bc=function_exists('bcscale');
    $range='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    if ($from_base==10)
    $base_10=$val;
    else
    {
    $n=strlen(($val="$val"))-++$ratio;
    if ($bc) for($i=$n;$i>-1;($ratio=bcmul($ratio,$from_base)) && $i--)
    $base_10=bcadd($base_10,bcmul(strpos($range,$val[$i]),$ratio));
    else for($i=$n;$i>-1;($ratio*=$from_base) && $i--)
    $base_10+=strpos($range,$val[$i])*$ratio;
    }
    if ($bc)
    do $result.=$range[bcmod($base_10,$to_base)];
    while(($base_10=bcdiv($base_10,$to_base))>=1);
    else
    do $result.=$range[$base_10%$to_base];
    while(($base_10/=$to_base)>=1);
    return strrev($to_base<37?strtolower($result):$result);
}


function toBase($num, $to_base=62) {
    $base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $r = $num  % $to_base ;
    $res = $base[$r];
    $q = floor($num/$to_base);
    while ($q) {
        $r = $q % $to_base;
        $q =floor($q/$to_base);
        $res = $base[$r].$res;
    }
    return $res;
}

function to10( $num, $b=62) {
    $base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $limit = strlen($num);
    $res=strpos($base,$num[0]);
    for($i=1;$i<$limit;$i++) {
        $res = $b * $res + strpos($base,$num[$i]);
    }
    return $res;
}


function ar_base_convert($num,$from_base=10,$to_base=62){
  $base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  if ($to_base>$from_base) {
      $r = $num  % $to_base ;
      $res = $base[$r];
      $q = floor($num/$to_base);
      while ($q) {
        $r = $q % $to_base;
        $q =floor($q/$to_base);
        $res = $base[$r].$res;
      }
  }
  else if ($to_base<$from_base) {
      $limit = strlen($num);
      $res=strpos($base,$num[0]);
      for($i=1;$i<$limit;$i++) {
        $res = $from_base * $res + strpos($base,$num[$i]);
      }

  }
  else if ($to_base==$from_base) {
      $res=$val;
  }
  return $res;
}
 

 

for ($i=1;$i<10;$i++) {
    $num=rand(100000,100000000000);
    $out=int2base($num);
    $num2=base2int($out);
    echo "$num -> $out -> $num2  LEN:".strlen($out);
    echo "<BR>";

    $out = base_convert_alt($num, 10,62);
    $num2 = base_convert_alt($out, 62,10);

    echo "$num -> $out -> $num2 LEN:".strlen($out);
    echo "<BR>";

    $out = toBase($num);
    $num2 = to10($out);

    echo "$num -> $out -> $num2 LEN:".strlen($out);
    echo "<BR>";


    $out = ar_base_convert($num,10,62);
    $num2 = ar_base_convert($out,62,10);

    echo "$num -> $out -> $num2 LEN:".strlen($out);
    echo "<BR>";

    
    echo "<BR>";
}
 

 

?>

 

 

 

67254498163 -> HIRLXFOV -> 67254498163 LEN:8
67254498163 -> 1bpv1gn -> 67254498163 LEN:7
67254498163 -> 1bpv1gn -> 67254498163 LEN:7
67254498163 -> 1bpv1gn -> 67254498163 LEN:7

86492069029 -> JSYPLOQP -> 86492069029 LEN:8
86492069029 -> 1wppUfr -> 86492069029 LEN:7
86492069029 -> 1wppUfr -> 86492069029 LEN:7
86492069029 -> 1wppUfr -> 86492069029 LEN:7

46292800946 -> ESVFLJTC -> 46292800946 LEN:8
46292800946 -> OwTSfM -> 46292800946 LEN:6
46292800946 -> OwTSfM -> 46292800946 LEN:6
46292800946 -> OwTSfM -> 46292800946 LEN:6

74768014582 -> IGZVSMVO -> 74768014582 LEN:8
74768014582 -> 1jBYY74 -> 74768014582 LEN:7
74768014582 -> 1jBYY74 -> 74768014582 LEN:7
74768014582 -> 1jBYY74 -> 74768014582 LEN:7

85146602204 -> JOPJEFCO -> 85146602204 LEN:8
85146602204 -> 1uWmsRC -> 85146602204 LEN:7
85146602204 -> 1uWmsRC -> 85146602204 LEN:7
85146602204 -> 1uWmsRC -> 85146602204 LEN:7

추천
0

댓글 0개

전체 30 |RSS
개발자 메모장 내용 검색

회원로그인

진행중 포인트경매

  1. 참여2 회 시작24.04.25 20:23 종료24.05.02 20:23
(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT