-
Notifications
You must be signed in to change notification settings - Fork 1
/
bstr2bin-bin2bstr.php
50 lines (40 loc) · 1002 Bytes
/
bstr2bin-bin2bstr.php
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
<?php
function bin2bstr($input)
// Convert a binary expression (e.g., "100111") into a binary-string
{
if (!is_string($input)) return null; // Sanity check
// Pack into a string
$input = str_split($input, 4);
$str = '';
foreach ($input as $v)
{
$str .= base_convert($v, 2, 16);
}
$str = pack('H*', $str);
return $str;
}
function bstr2bin($input,$flag='2')
// Binary representation of a binary-string
{
if (!is_string($input)) return null; // Sanity check
// Unpack as a hexadecimal string
$value = unpack('H*', $input);
// Output binary representation
$value = str_split($value[1], 1);
$bin = '';
foreach ($value as $v)
{
$b = str_pad(base_convert($v, 16, 2), 4, '0', STR_PAD_LEFT);
if($flag=='16')
{
$b = dechex(bindec($b));
}
$bin .= $b;
}
return $bin;
}
//test
$content = file_get_contents('/tmp/test.png');
$content = bstr2bin($content,'16');
//$content = bin2bstr($content);
var_dump($content);exit();