|
@@ -9,18 +9,18 @@ namespace loyalsoft;
|
|
* @author gwang (mail@wanggangzero.cn)
|
|
* @author gwang (mail@wanggangzero.cn)
|
|
* @copyright ? 2017-6-12, SJZ LoyalSoft Corporation & gwang. All rights reserved.
|
|
* @copyright ? 2017-6-12, SJZ LoyalSoft Corporation & gwang. All rights reserved.
|
|
*/
|
|
*/
|
|
-class Bytes
|
|
|
|
-{
|
|
|
|
|
|
+class Bytes {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 转换一个String字符串为byte数组
|
|
* 转换一个String字符串为byte数组
|
|
* @param $str 需要转换的字符串
|
|
* @param $str 需要转换的字符串
|
|
* @param $bytes 目标byte数组
|
|
* @param $bytes 目标byte数组
|
|
*/
|
|
*/
|
|
- public static function getBytes($string)
|
|
|
|
- {
|
|
|
|
|
|
+ public static function getBytes(string $string): array {
|
|
$bytes = array();
|
|
$bytes = array();
|
|
- for ($i = 0; $i < strlen($string); $i++) {
|
|
|
|
|
|
+ for ($i = 0;
|
|
|
|
+ $i < strlen($string);
|
|
|
|
+ $i++) {
|
|
$bytes[] = ord($string[$i]);
|
|
$bytes[] = ord($string[$i]);
|
|
}
|
|
}
|
|
return $bytes;
|
|
return $bytes;
|
|
@@ -32,8 +32,7 @@ class Bytes
|
|
* @param $str 目标字符串
|
|
* @param $str 目标字符串
|
|
* @return 一个String类型的数据
|
|
* @return 一个String类型的数据
|
|
*/
|
|
*/
|
|
- public static function toStr($bytes)
|
|
|
|
- {
|
|
|
|
|
|
+ public static function toStr(array $bytes): string {
|
|
$str = '';
|
|
$str = '';
|
|
foreach ($bytes as $ch) {
|
|
foreach ($bytes as $ch) {
|
|
$str .= chr($ch);
|
|
$str .= chr($ch);
|
|
@@ -47,8 +46,7 @@ class Bytes
|
|
* @param $byt 目标byte数组
|
|
* @param $byt 目标byte数组
|
|
* @param $val 需要转换的字符串
|
|
* @param $val 需要转换的字符串
|
|
*/
|
|
*/
|
|
- public static function integerToBytes($val)
|
|
|
|
- {
|
|
|
|
|
|
+ public static function integerToBytes(int $val): array {
|
|
$byt = array();
|
|
$byt = array();
|
|
$byt[0] = ($val & 0xff);
|
|
$byt[0] = ($val & 0xff);
|
|
$byt[1] = ($val >> 8 & 0xff);
|
|
$byt[1] = ($val >> 8 & 0xff);
|
|
@@ -63,8 +61,7 @@ class Bytes
|
|
* @param $position 指定的开始位置
|
|
* @param $position 指定的开始位置
|
|
* @return 一个Integer类型的数据
|
|
* @return 一个Integer类型的数据
|
|
*/
|
|
*/
|
|
- public static function bytesToInteger($bytes, $position)
|
|
|
|
- {
|
|
|
|
|
|
+ public static function bytesToInteger(array $bytes, int $position): int {
|
|
// $val = 0;
|
|
// $val = 0;
|
|
$val = $bytes[$position + 3] & 0xff;
|
|
$val = $bytes[$position + 3] & 0xff;
|
|
$val <<= 8;
|
|
$val <<= 8;
|
|
@@ -81,8 +78,7 @@ class Bytes
|
|
* @param $byt 目标byte数组
|
|
* @param $byt 目标byte数组
|
|
* @param $val 需要转换的字符串
|
|
* @param $val 需要转换的字符串
|
|
*/
|
|
*/
|
|
- public static function shortToBytes($val)
|
|
|
|
- {
|
|
|
|
|
|
+ public static function shortToBytes(int $val): array {
|
|
$byt = array();
|
|
$byt = array();
|
|
$byt[0] = ($val & 0xff);
|
|
$byt[0] = ($val & 0xff);
|
|
$byt[1] = ($val >> 8 & 0xff);
|
|
$byt[1] = ($val >> 8 & 0xff);
|
|
@@ -95,13 +91,11 @@ class Bytes
|
|
* @param $position 指定的开始位置
|
|
* @param $position 指定的开始位置
|
|
* @return 一个Short类型的数据
|
|
* @return 一个Short类型的数据
|
|
*/
|
|
*/
|
|
- public static function bytesToShort($bytes, $position)
|
|
|
|
- {
|
|
|
|
|
|
+ public static function bytesToShort(array $bytes, int $position): int {
|
|
// $val = 0;
|
|
// $val = 0;
|
|
$val = $bytes[$position + 1] & 0xFF;
|
|
$val = $bytes[$position + 1] & 0xFF;
|
|
$val <<= 8;
|
|
$val <<= 8;
|
|
$val |= $bytes[$position] & 0xFF;
|
|
$val |= $bytes[$position] & 0xFF;
|
|
return $val;
|
|
return $val;
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|