Below we together to see and an article about PHP BCD code compression-The decimal number compressed into hexadecimal data instances, I hope that the article to help you students oh.
Example, PHP BCD compression-Compress decimal numbers into hexadecimal data
The code is as follows |
Copy Code |
/* PHP BCD compression-Compress decimal numbers into hexadecimal data For example 0091 after compression 0x00 0x91 */ $string = ' 0091 '; $bytes = Bytes::getbytes ($string); Print_r ($bytes); /* Array (
[0] = 48
[1] = 48
[2] = 57
[3] = 49 ) */ $asc =bytes::asctohex ($bytes, 4); 4-bit compression into 2-bit Print_r ($ASC); /* Array (
[0] = 0
[1] = 145 ) */ echo Bytes::tostr ($ASC); /* 0091 */ $hex =bytes::hextoasc ($ASC, 2); Anti-operation 2-bit revert to 4-bit Print_r ($hex); /* Array (
[0] = 48
[1] = 48
[2] = 57
[3] = 49 ) */ ?> |
example, compressing decimal numbers into hexadecimal data
The code is as follows |
Copy Code |
/** * PHP BCD Code compression * Compress decimal numbers into hexadecimal data * @author phpff.com * Created on 2011-7-15 */ Class Bytes { /** * Convert a string string to a byte array * @param $str strings that need to be converted * @param $bytes Target byte array * @author phpff.com */ public static function GetBytes ($string) { $bytes = Array (); for ($i = 0; $i < strlen ($string); $i + +) { $bytes [] = Ord ($string [$i]); } return $bytes; } /** * Converts a byte array to a string type of data * @param $bytes byte array * @param $str Target string * @return A String type of data */ public static function Tostr ($bytes) { $str = "; foreach ($bytes as $ch) { $str. = Bin2Hex (Chr ($ch)); } return $str; } /** * ASC code turns into 16 binary data * @param $asc ASC Numeric string * @param $AscLen the length of the string to convert * @return 16 binary array * @author phpff.com */ public static function Asctohex ($ASC, $AscLen) { $i = 0; $Hex =array (); for ($i = 0; $i < $AscLen; $i + +) { /*a:0x41 (0100 0001), a:0x61 (0110 0001), 4-bit right shift after 0001, plus 0x90, etc. 0xa*/ $Hex [$i] = (Chr ($asc [$i]) << 4); if (! ( Chr ($ASC [$i]) >= ' 0 ' && chr ($ASC [$i]) <= ' 9 ') { $Hex [$i] + = 0x90; } if ($i +1 >= $AscLen) { Break } $Hex [$i] |= (Chr ($ASC [$i +1]) & 0x0f); if (! ( Chr ($ASC [$i +1]) >= ' 0 ' && chr ($asc [$i +1]) <= ' 9 ') { $Hex [$i] + = 0x09; } } return $Hex; } /** * Convert 16 binary data into ASC code * @param $Hex 16 binary array * @param $HexLen 16 binary array length * @return ASC Array * @author phpff.com */ public static function Hextoasc ($Hex, $HexLen) { $i = 0; $Temp = 0; for ($i = 0; $i < $HexLen; $i + +) { $Temp = ($Hex [$i] & 0xf0) >> 4; if ($Temp < 10) { $ASC [$i] = (0x30 + $Temp); }else{ $ASC [$i] = (0x37 + $Temp); } $Temp = $Hex [$i] & 0x0f; if ($Temp < 10) { $ASC [$i +1] = (0x30 + $Temp); }else{ $ASC [$i +1] = (0x37 + $Temp); } } return $ASC; } } ?> |
http://www.bkjia.com/PHPjc/633105.html www.bkjia.com true http://www.bkjia.com/PHPjc/633105.html techarticle below we together to see and an article about PHP BCD code compression-The decimal number compressed into hexadecimal data instances, I hope that the article to help you students oh. Example, PHP BCD code compression-put 10 ...