PHP stringAnd
bytebyte array
TurnExample of a class
PHP
/**
* byteArray andString gotoof the class
*/
class Bytes {
/**
* Turnchange a stringstringto bebyteArray
* @param $str needTurnChange thestring
* @param $bytes targetbyteArray
* @author Zikie
*/
Public static function GetBytes ($string) {
$bytes = Array ();
For ($i = 0; $i < strlen ($string); $i + +) {
$bytes [] = Ord ($string [$i]);
}
return $bytes;
}
/**
* The byte arrayTurnto a string type of data
* @param $bytes byte array
* @param $str targetstring
* @return A String type of data
*/
Public static function Tostr ($bytes) {
$str = ";
foreach ($bytes as $ch) {
$str. = Chr ($ch);
}
return $str;
}
/**
* Turnchange an int tobyteArray
* @param $byt targetbyteArray
* @param $val needTurnChange thestring
*
*/
Public static function Integertobytes ($val) {
$byt = Array ();
$byt [0] = ($val & 0xff);
$byt [1] = ($val >> 8 & 0xff);
$byt [2] = ($val >> & 0xff);
$byt [3] = ($val >> & 0xff);
return $byt;
}
/**
* Reads an integer type of data from the location specified in the byte array
* @param $bytes byte array
* @param $position The starting position specified
* @return An integer type of data
*/
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;
}
/**
* Turnchange a Shor .stringto bebyteArray
* @param $byt targetbyteArray
* @param $val needTurnChange thestring
*
*/
Public static function Shorttobytes ($val) {
$byt = Array ();
$byt [0] = ($val & 0xff);
$byt [1] = ($val >> 8 & 0xff);
return $byt;
}
/**
* Reads a short type of data from the location specified in the byte array.
* @param $bytes byte array
* @param $position The starting position specified
* @return A short type of data
*/
Public static function Bytestoshort ($bytes, $position) {
$val = 0;
$val = $bytes [$position + 1] & 0xFF;
$val = $val << 8;
$val |= $bytes [$position] & 0xFF;
return $val;
}
}
?>
The above describes the PHP string and byte array conversion Class example, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.