For example, compress the php bcd code-compress the decimal number to the hexadecimal data.
The code is as follows: |
Copy code |
<? Php /* Php bcd code compression-compress decimal numbers into hexadecimal data For example, 0x00 0x91 after 0091 compression */ $ String = '000000 '; $ 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 ); // Reverse operation 2-bit to 4-bit Print_r ($ hex ); /* Array ( [0] => 48 [1] => 48 [2] => 57 [3] => 49 ) */ ?> |
For example, compress the decimal number into the hexadecimal data.
The code is as follows: |
Copy code |
<? Php /** * Php bcd code compression * Compress the decimal number to the hexadecimal data. * @ Author phpff.com * Created on 2011-7-15 */ Class Bytes { /** * Convert a String to a byte array. * @ Param $ str the string 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; } /** * Convert byte arrays to String-type data. * @ Param $ bytes byte array * @ Param $ str target string * @ Return a String type data */ Public static function toStr ($ bytes ){ $ Str = ''; Foreach ($ bytes as $ ch ){ $ Str. = bin2hex (chr ($ ch )); } Return $ str; } /** * Convert asc code to hexadecimal data * @ Param $ asc numeric string * @ Param $ AscLen the length of the string to be converted * @ Return hexadecimal array * @ Author phpff.com */ Public static function AscToHex ($ asc, $ AscLen ){ $ I = 0; $ Hex = array (); For ($ I = 0; 2 * $ I <$ AscLen; $ I ++) { /* A: 0x41 (0100 0001), a: 0x61 (0110 0001), 0001 after four digits shifted to the right, and 0 XA such as 0x90 */ $ Hex [$ I] = (chr ($ asc [2 * $ I]) <4 ); If (! (Chr ($ asc [2 * $ I])> = '0' & chr ($ asc [2 * $ I]) <= '9 ')){ $ Hex [$ I] + = 0x90; } If (2 * $ I + 1 >=$ AscLen ){ Break; } $ Hex [$ I] | = (chr ($ asc [2 * $ I + 1]) & 0x0f ); If (! (Chr ($ asc [2 * $ I + 1])> = '0' & chr ($ asc [2 * $ I + 1]) <= '9 ')){ $ Hex [$ I] + = 0x09; } } Return $ Hex; } /** * Convert hexadecimal data to asc code * @ Param $ Hex hexadecimal array * @ Param $ HexLen hexadecimal 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 [2 * $ I] = (0x30 + $ Temp ); } Else { $ Asc [2 * $ I] = (0x37 + $ Temp ); } $ Temp = $ Hex [$ I] & 0x0f; If ($ Temp <10 ){ $ Asc [2 * $ I + 1] = (0x30 + $ Temp ); } Else { $ Asc [2 * $ I + 1] = (0x37 + $ Temp ); } } Return $ Asc; } } ?> |