Phpbcd code compression-compressing decimal numbers into hexadecimal data _ PHP Tutorial

Source: Internet
Author: User
Phpbcd code compression-compress decimal numbers into hexadecimal data. Next, let's take a look at the example about how to compress the phpbcd code to compress the decimal number into the hexadecimal data. I hope this article will help you. For example, compress the phpbcd code-let's take a look at the following section with regard to php bcd code compression-compress the decimal number to the hexadecimal data instance, I hope the article will help you.

For example, compress the php bcd code-compress the decimal number to the hexadecimal data.

The code is as follows:
/*
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:

/**

* 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;
}

}
?>

Compress the bcd code-compress the decimal number to the hexadecimal data instance. I hope this article will help you. For example, compress the php bcd code...

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.