本文操作环境:windows7系统、PHP7.1版、DELL G3电脑php怎么实现转成byte数组?php转换byte[]数据类型代码如下:<?php class Bytes { public static fun
本文操作环境:windows7系统、PHP7.1版、DELL G3电脑
php怎么实现转成byte数组?
php转换byte[]数据类型
代码如下:
<?php
class Bytes {
public static function getbytes($str) {
$len = strlen($str);
$bytes = array();
for($i=0;$i<$len;$i++) {
if(ord($str[$i]) >= 128){
$byte = ord($str[$i]) - 256;
}else{
$byte = ord($str[$i]);
}
$bytes[] = $byte ;
}
return $bytes;
}
public static function tostr($bytes) {
$str = '';
foreach($bytes as $ch) {
$str .= chr($ch);
}
return $str;
}
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;
}
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;
}
public static function shorttobytes($val) {
$byt = array();
$byt[0] = ($val & 0xff);
$byt[1] = ($val >> 8 & 0xff);
return $byt;
}
public static function bytestoshort($bytes, $position) {
$val = 0;
$val = $bytes[$position + 1] & 0xff;
$val = $val << 8;
$val |= $bytes[$position] & 0xff;
return $val;
}
}
--结束END--
本文标题: php怎么实现转成byte数组
本文链接: https://www.lsjlt.com/news/1081.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0