-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexFileConverter.php
More file actions
56 lines (52 loc) · 1.12 KB
/
HexFileConverter.php
File metadata and controls
56 lines (52 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* php 文件与16进制串转换器
*
* @author fdipzone
* @DateTime 2023-03-21 20:03:12
*
*/
class HexFileConverter
{
/**
* 文件转为16进制串
*
* @author fdipzone
* @DateTime 2023-03-21 20:04:59
*
* @param string $file 文件
* @return string
*/
public static function fileToHex(string $file):string
{
if(!file_exists($file))
{
throw new \Exception('file not exists');
}
$data = file_get_contents($file);
return bin2hex($data);
}
/**
* 16进制串转为文件
*
* @author fdipzone
* @DateTime 2023-03-21 20:05:29
*
* @param string $hex_str 16进制串
* @param string $file 生成的文件
* @return boolean
*/
public static function hexToFile(string $hex_str, string $file):bool
{
try
{
$data = pack('H*', $hex_str);
file_put_contents($file, $data, true);
}
catch(\Throwable $e)
{
throw new \Exception('hex str convert file fail');
}
return true;
}
}