MathConverter::hexToBin()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace cse\helpers;
6
7
/**
8
 * Class MathConverter
9
 *
10
 * @package cse\helpers
11
 */
12
class MathConverter
13
{
14
    const DEFAULT_DECIMAL = 2;
15
16
    /**
17
     * Convert hex to binary
18
     *
19
     * @param string $hex
20
     *
21
     * @return string
22
     */
23
    public static function hexToBin(string $hex): string
24
    {
25
        return pack('H*' , $hex);
26
    }
27
28
    /**
29
     * Convert binary ot hex
30
     *
31
     * @param string $bin
32
     *
33
     * @return string
34
     */
35
    public static function binToHex(string $bin): string
36
    {
37
        $hex = '';
38
        $i = 0;
39
        do {
40
            $hex .= sprintf('%02x', ord($bin{$i}));
41
            $i++;
42
        } while ($i < strlen($bin));
43
44
        return $hex;
45
    }
46
47
    /**
48
     * Convert megabytes to bytes
49
     *
50
     * @param $megaBytes
51
     *
52
     * @return mixed
53
     */
54
    public static function mbToBytes($megaBytes)
55
    {
56
        return $megaBytes * pow(1024, 2);
57
    }
58
59
    /**
60
     * Convert bytes to megabytes
61
     *
62
     * @param int $bytes
63
     * @param int $decimal
64
     *
65
     * @return float
66
     */
67
    public static function bytesToMb(int $bytes, int $decimal = self::DEFAULT_DECIMAL): float
68
    {
69
        return round($bytes / pow(1024, 2), $decimal);
70
    }
71
72
    /**
73
     * Convert gigabytes to bytes
74
     *
75
     * @param $gigaBytes
76
     *
77
     * @return mixed
78
     */
79
    public static function gbToBytes($gigaBytes)
80
    {
81
        return $gigaBytes * pow(1024, 3);
82
    }
83
84
    /**
85
     * Convert bytes to gigabytes
86
     *
87
     * @param int $bytes
88
     * @param int $decimal
89
     *
90
     * @return float
91
     */
92
    public static function bytesToGb(int $bytes, int $decimal = self::DEFAULT_DECIMAL): float
93
    {
94
        return round($bytes / pow(1024, 3), $decimal);
95
    }
96
97
    /**
98
     * Convert gigabytes to megabytes
99
     *
100
     * @param $gigaBytes
101
     *
102
     * @return mixed
103
     */
104
    public static function gbToMb($gigaBytes)
105
    {
106
        return $gigaBytes * 1024;
107
    }
108
109
    /**
110
     * Convert megabytes to gigabytes
111
     *
112
     * @param $megaBytes
113
     * @param int $decimal
114
     *
115
     * @return float
116
     */
117
    public static function mbToGb($megaBytes, int $decimal = self::DEFAULT_DECIMAL): float
118
    {
119
        return round($megaBytes / 1024, $decimal);
120
    }
121
122
    /**
123
     * Convert to megabyte
124
     *
125
     * @param string $value
126
     *
127
     * @return float|int|string
128
     */
129
    public static function toMb(string $value)
130
    {
131
        $value = trim($value);
132
        $units = strtolower($value[strlen($value) - 1]);
133
        $value = floatval($value);
134
        switch ($units) {
135
            case 'p':
136
                $value *= pow(1024, 3);
137
                break;
138
            case 't':
139
                $value *= pow(1024, 2);
140
                break;
141
            case 'g':
142
                $value *= 1024;
143
                break;
144
            case 'm':
145
                break;
146
            case 'k':
147
                $value /= 1024;
148
                break;
149
            case 'b':
150
                $value /= pow(1024, 2);
151
        }
152
153
        return $value;
154
    }
155
156
    /**
157
     * Cut decimal
158
     *
159
     * @param $number
160
     * @param int $decimal
161
     * @param string $delimiter
162
     *
163
     * @return float
164
     */
165
    public static function cutDecimal($number, int $decimal = self::DEFAULT_DECIMAL, string $delimiter = '.'): float
166
    {
167
        $format = explode($delimiter, (string) $number);
168
169
        if (empty($format[1]) || $decimal == strlen($format[1])) return (float) $number;
170
        $format[1] = substr($format[1], 0, $decimal - strlen($format[1]));
171
172
        return (float) implode($delimiter, $format);
173
    }
174
175
    /**
176
     * Round decimal
177
     *
178
     * @param $number
179
     * @param int $decimal
180
     *
181
     * @return float
182
     */
183
    public static function roundDecimal($number, int $decimal = self::DEFAULT_DECIMAL): float
184
    {
185
        $delimiter = pow(10, $decimal);
186
        return round(ceil($number * $delimiter) / $delimiter, $decimal);
187
    }
188
}