10-binary and 64-in-binary mutual transfer

Source: Internet
Author: User
Tags numeric md5 strlen
Previously wrote 16 binary and 64 binary conversion function, today to do download page short connection, suddenly thought can be converted to 64 binary ID, so that the address looks shorter, below is the code, share to everyone.     Performance test, 1000-time calculation of the reciprocal calculation, 4-bit 64 binary number (aaaa:2663050), 100 milliseconds or so, the average one conversion time of about 50 microseconds. <?php
Class Jinzhi {
Const KEYCODE = ' 0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz_$ ';

/**
* Converts a 64-digit string into a 10-binary numeric string
* @param string of $m string 64 binary
* @param $len Integer Returns the length of the string, if the length is not sufficient with 0 padding, 0 is not populated
* @return String
* @author Mustang
*/
Public Function Hex64to10 ($m, $len = 0) {
$m = (string) $m;
$hex 2 = ";
$Code = self:: keycode;
for ($i = 0, $l = strlen ($Code); $i < $l; $i + +) {
$KeyCode [] = $Code [$i];
}
$KeyCode = Array_flip ($KeyCode);

for ($i = 0, $l = strlen ($m); $i < $l; $i + +) {
$one = $m [$i];
$hex 2. = Str_pad (Decbin ($KeyCode [$one]), 6, ' 0 ', str_pad_left);
}
$return = Bindec ($hex 2);

if ($len) {
$clen = strlen ($return);
if ($clen >= $len) {
return $return;
}
else {
Return Str_pad ($return, $len, ' 0 ', str_pad_left);
}
}
return $return;
}

/**
* Converts a 10-digit string into a 64-binary numeric string
* @param string of $m string 10 binary
* @param $len Integer Returns the length of the string, if the length is not sufficient with 0 padding, 0 is not populated
* @return String
* @author Mustang
*/
Public Function hex10to64 ($m, $len = 0) {
$KeyCode = self:: keycode;
$hex 2 = Decbin ($m);
$hex 2 = $this, Str_rsplit ($hex 2, 6);
$hex = Array ();
foreach ($hex 2 as $one) {
$t = Bindec ($one);
$hex [] = $KeyCode [$t];
}
$return = preg_replace ('/^0*/', ' ', implode (', $hex 64));
if ($len) {
$clen = strlen ($return);
if ($clen >= $len) {
return $return;
}
else {
Return Str_pad ($return, $len, ' 0 ', str_pad_left);
}
}
return $return;
}

/**
* Converts a 16-digit string into a 64-binary numeric string
* @param string of $m string 16 binary
* @param $len Integer Returns the length of the string, if the length is not sufficient with 0 padding, 0 is not populated
* @return String
* @author Mustang
*/
protected function hex16to64 ($m, $len = 0) {
$KeyCode = self:: keycode;
$hex 2 = array ();
for ($i = 0, $j = strlen ($m), $i < $j; + + $i) {
$hex 2 [] = Str_pad (Base_convert ($m [$i], 2), 4, ' 0 ', str_pad_left);
}
$hex 2 = Implode ("', $hex 2);
$hex 2 = $this, Str_rsplit ($hex 2, 6);
foreach ($hex 2 as $one) {
$hex [] = $KeyCode [Bindec ($one)];
}
$return = preg_replace ('/^0*/', ' ', implode (', $hex 64));
if ($len) {
$clen = strlen ($return);
if ($clen >= $len) {
return $return;
}
else {
Return Str_pad ($return, $len, ' 0 ', str_pad_left);
}
}
return $return;
}

/**
* Function and PHP native function Str_split Close, just start counting cutting from the tail
* @param string to be cut $str string
* @param $len length of each string of integers
* @return Array
* @author Mustang
*/
protected function Str_rsplit ($str, $len = 1) {
if ($str = = NULL | | $str = = FALSE | | $str = = ") return false;
$strlen = strlen ($STR);
if ($strlen <= $len) return Array ($STR);
$headlen = $strlen% $len;
if ($headlen = = 0) {
Return Str_split ($str, $len);
}
$return = Array (substr ($str, 0, $headlen));
Return Array_merge ($return, Str_split (substr ($str, $headlen), $len));
}
}
?>


At work, we usually do. Encryption password, authorization certificate, file fingerprint and so on for storage and use of the most is directly used MD5 value, MD5 value is a 32-bit long 16 binary integer, in fact, we can bar this 16 binary number into 64 binary, to achieve the same function, Shorten the string length (the 32-bit 16-MD5 value can be compressed to 22-bit 64 decimal). As far as shortening length is useful, I think you know O (∩_∩) o~
<?php
echo "\ r \ n <br/>1:". Hex16to64 (' 1 ');
echo "\ r \ n <br/>2:". Hex16to64 (' 2 ');
echo "\ r \ n <br/>3:". Hex16to64 (' 3 ');
echo "\ r \ n <br/>4:". Hex16to64 (' 4 ');
echo "\ r \ n <br/>5:". Hex16to64 (' 5 ');
echo "\ r \ n <br/>6:". Hex16to64 (' 6 ');
echo "\ r \ n <br/>7:". Hex16to64 (' 7 ', 5);
The echo "\ r \ n <br/>8:". Hex16to64 (' 8 ');
Ten echo "\ r \ n <br/>9:". Hex16to64 (' 9 ');
echo "\ r \ n <br/>a:"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.