base62_encode|base64_decode的P
完全参照Java 改写了一版本PHP,希望能提供方便。
创新互联从2013年开始,是专业互联网技术服务公司,拥有项目网站设计、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元彰武做网站,已为上家服务,为彰武各地企业和个人服务,联系电话:18982081108
5) { char c = encodes[val >> (pos -= 6)]; sb.append( c == 'i' ? "ia" : (c == '+' ? "ib" :( c == '/' ? "ic" : c))); val &= ((1 << pos) - 1); } } if (pos > 0) { char c = encodes[val << (6 - pos)]; sb.append( c == 'i' ? "ia" : (c == '+' ? "ib" :( c == '/' ? "ic" : c))); } return sb; } */ public static function base62_encode($data) { $sb = ''; $len = strlen($data); $pos = $val = 0; for($i= 0; $i < $len; $i++){ $tv = substr($data, $i, 1); $val = ($val << 8) | (ord($tv) & 0xFF); $pos += 8; while($pos > 5){ $c = substr(self::$encodes, $val >> ($pos -= 6), 1); $sb .= $c == 'i' ? "ia" : (($c == '+') ? "ib" : (($c == '/') ? 'ic' : $c)); $val &= ((1<<$pos) -1); } } if($pos > 0){ $c = substr(self::$encodes, $val << (6 - $pos), 1); $sb .= ($c == 'i') ? "ia" : (($c == '+') ? "ib" : (($c == '/') ? 'ic' : $c)); } return $sb; } /* public static byte[] decodeBase62(char[] data) { ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length); int pos = 0, val = 0; for (int i = 0; i < data.length; i++) { char c = data[i]; if (c == 'i') { c = data[++i]; c = c == 'a' ? 'i' : (c == 'b' ? '+' : (c == 'c' ? '/' : data[--i])); } val = (val << 6) | decodes[c]; pos += 6; while (pos > 7) { baos.write(val >> (pos -= 8)); val &= ((1 << pos) - 1); } } return baos.toByteArray(); } */ public static function base62_decode($data) { $baos = ''; $pos = $val = 0; $len = strlen($data); for($i = 0; $i < $len; $i++){ $c = substr($data, $i, 1); if($c == 'i'){ $c = substr($data, ++$i, 1); $c = ($c == 'a') ? 'i' : ($c == 'b' ? '+' : ($c == 'c' ? '/' : substr($data, --$i, 1))); } $val = ($val << 6) | strpos(self::$encodes, $c); $pos += 6; while($pos > 7){ $tv = ($val >> ($pos -= 8)); //var_dump($tv); $baos .= chr($tv); $val &= ((1 << $pos) - 1); } } return $baos; } } foreach([1,2,3,'a','b', 'c', 'addddd', '111', 'fkdskfjdsljflsdjfljsaldkjflaskjfldskjlfkjdslkfjladskjfldsk'] as $v){ var_dump($v); $b = Base62::base62_encode($v); var_dump($b); var_dump(base64_encode($v)); var_dump(Base62::base62_decode($b)); echo "----------------------\n"; }
分享题目:base62_encode|base64_decode的P
网站路径:http://myzitong.com/article/jsjgdh.html