Php string and byte array conversion class example _ PHP Tutorial

Source: Internet
Author: User
Php string and byte array conversion class example. This article provides a php string and byte array conversion class example. I hope this article will be helpful to you. The code is as follows: Copy the code? Php *** byte array and string conversion example provides a php string and byte array conversion class. I hope this article will be helpful to you.
The code is as follows:

/**

* Byte array and string conversion class

*/

Class Bytes {


/**

* Convert a String to a byte array.

* @ Param $ str the string to be converted

* @ Param $ bytes target byte array

* @ Author Zikie

*/
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. = chr ($ ch );
}

Return $ str;
}


/**

* Convert an int to a byte array.

* @ Param $ byt the target byte array

* @ Param $ val: string to be converted

*

*/

Public static function integerToBytes ($ val ){
$ Byt = array ();
$ Byt [0] = ($ val & 0xff );
$ Byt [1] = ($ val> 8 & 0xff );
$ Byt [2] = ($ val> 16 & 0xff );
$ Byt [3] = ($ val> 24 & 0xff );
Return $ byt;
}


/**

* Read an Integer data from the specified position in the byte array

* @ Param $ bytes byte array

* @ Param $ position specifies the start position.

* @ Return an Integer data type

*/

Public static function bytesToInteger ($ bytes, $ position ){
$ Val = 0;
$ Val = $ bytes [$ position + 3] & 0xff;
$ Val <= 8;
$ Val | = $ bytes [$ position + 2] & 0xff;
$ Val <= 8;
$ Val | = $ bytes [$ position + 1] & 0xff;
$ Val <= 8;
$ Val | = $ bytes [$ position] & 0xff;
Return $ val;
}


/**

* Convert a shor string to a byte array.

* @ Param $ byt the target byte array

* @ Param $ val: string to be converted

*

*/

Public static function extends tobytes ($ val ){
$ Byt = array ();
$ Byt [0] = ($ val & 0xff );
$ Byt [1] = ($ val> 8 & 0xff );
Return $ byt;
}


/**

* Read a Short type data from the specified position in the byte array.

* @ Param $ bytes byte array

* @ Param $ position specifies the start position.

* @ Return a Short type data

*/

Public static function bytesToShort ($ bytes, $ position ){
$ Val = 0;
$ Val = $ bytes [$ position + 1] & 0xFF;
$ Val = $ val <8;
$ Val | = $ bytes [$ position] & 0xFF;
Return $ val;
}

}
?>

Bytes. The code is as follows? Php/*** Convert byte array and string...

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.