Skip to content

Commit

Permalink
add StringHelper::bytes2Str
Browse files Browse the repository at this point in the history
  • Loading branch information
kakuilan committed Oct 24, 2021
1 parent 1bbe5bf commit 62f439c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,23 @@ public static function toBytes(string $str): array {
}


/**
* 字节数组转字符串
* @param array $arr
* @return string
*/
public static function bytes2Str(array $arr): string {
$res = '';
$len = count($arr);
for ($i = 0; $i < $len; $i++) {
$cod = intval($arr[$i]);
$res .= chr($cod);
}

return $res;
}


/**
* 检查字符串 $str 是否包含数组$arr的元素之一
* @param string $str
Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,15 @@ public function testGrapBrackets() {
}


public function testToBytes() {
$str = "Hello World! 你好,世界!Olá🐍 With Emojis 🐳📜";

$res1 = StringHelper::toBytes($str);
$res2 = StringHelper::bytes2Str($res1);

$this->assertEquals(count($res1), strlen($str));
$this->assertEquals($str, $res2);
}


}

0 comments on commit 62f439c

Please sign in to comment.