integer-2-base64

Gesponsorde koppelingen

PHP script bestanden

  1. integer-2-base64

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?
class base64 {
    public static $BASE64ENC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
    public static $BASE64DEC = array();
    
    /*
    Function to encode or decode an INT to/from BASE64.
    INPUT:
        $int: MIXED representing an INT or a STRING.
            When an INT is given, it will be encoded to base64.
            When a string is given, it will be decoded as base64.
        $length: INT representing the fixed length of the base64 output. Only used when an INT is given as first argument.
    */

    public function int($target,$length = 0) {
        if(is_string($target)) {
            return self::int_decode($target);
        }
elseif(is_int($target)) {
            return self::int_encode($target,$length);
        }
    }

    
    /*
    Function to encode/decode multiple integers based on the multi-int function.
    INPUT:
        $target: MIXED representing an INT or an ARRAY.
            When an INT is given, it will be extracted to multiple ints. OUTPUT will be an ARRAY containing the integers.
            When an ARRAY is given, it will be stored to a single int. OUTPUT will be an INT.
        $layout: ARRAY containing multiple integers representing the reserved amount of bits per integer.

    OUTPUT:
        MIXED: ARRAY or INT.
    
    NOTES:
        -It's recommended to store a multiply of 6 bits. (6, 12, 18, 24 or 30 bits)
        -Try not to store more then 30 bits of data.
        -For every 6 bits, a single BASE64 character will be returned.
    */

    public function multiint($target,$layout) {
        if(is_string($target)) {
            return multiint(self::int_decode($target),$layout);
        }
elseif(is_array($target)) {
            $length = ceil(array_sum($layout)/6);
            return self::int_encode(multiint($target,$layout),$length);            
        }
    }

    
    /*
    Function to encode an int to a BASE64 string.
    Input:
        $int:      INT representing the integer to encode.
        $length: INT respresenting the fixed length of the BASE64 output.
        
    Output:
        STRING representing the BASE64 encoded int.
    */

    public function int_encode($int,$length = 0) {
        $BASE64ENC = self::$BASE64ENC;
        $output = '';
        $power = 1;
        do {
            $power *= 64;
            $substract = $int%$power;
            $output = $BASE64ENC[(int)($substract/($power/64))] . $output;
        }
while($int/$power >= 1);
        
        if($length) {
            $output = substr(str_pad($output,$length,$BASE64ENC[0],STR_PAD_LEFT),-$length);
        }

        
        return $output;
    }


    /*
    Function to decode an int to a BASE64 string.
    Input:
        $base64: STRING representing the BASE64 encoded int.
        
    Output:
        INT representing the decoded int.
    */

    public function int_decode($base64) {
        $BASE64DEC = self::$BASE64DEC;
        $base64 = strrev((string)$base64);
        $int=0;
        $power = 1;
        for($x=0;$x<strlen($base64);$x++) {
            $int += $power*$BASE64DEC[$base64[$x]];
            $power *= 64;
        }

        return $int;
    }
}

base64::$BASE64DEC = array_flip(str_split(base64::$BASE64ENC));
?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.