3v4l.org

run code in 300+ PHP versions simultaneously
<?php // https://stackoverflow.com/questions/35536141/converting-binary-data-to-ascii-with-php-any-possibility/35536281#35536281 function bin2ascii($input) { $output = ''; for($i=0; $i<strlen($input); $i+=8) { $output .= chr(intval(substr($input, $i, 8), 2)); } return $output; } https://stackoverflow.com/questions/6382738/convert-string-to-binary-then-back-again-using-php/6382880#6382880 function ascii2bin($input) { //return array_reduce(str_split($input), fn ($acc, $i) => $acc .= decbin(ord($i))); return array_reduce(str_split($input), fn ($acc, $i) => $acc .= str_pad(decbin(ord($i)), 8, '0', STR_PAD_LEFT)); } // https://stackoverflow.com/questions/11946696/php-signed-binary-strings/11946971#11946971 function bindec2($bin) { if (strlen($bin) == 64 && $bin[0] == '1') { for ($i = 0; $i < 64; $i++) { $bin[$i] = $bin[$i] == '1' ? '0' : '1'; } return (bindec($bin) + 1) * -1; } return bindec($bin); } $i = -114514; var_dump(decbin($i)); $trimmedPadding = ltrim(decbin($i), $i < 0 ? '1' : '0'); var_dump($trimmedPadding); $signBitsPading = $i < 0 ? str_repeat('1', 8 - strlen($trimmedPadding) % 8) : str_repeat('0', 8 - strlen($trimmedPadding) % 8); var_dump($signBitsPading . $trimmedPadding); var_dump(bin2hex(bin2ascii($signBitsPading . $trimmedPadding))); $o = rtrim(base64_encode(bin2ascii($signBitsPading . $trimmedPadding)), '='); var_dump($o); $d = base64_decode($o); var_dump(bin2hex($d)); $bits = ascii2bin($d); $signBit = $bits[0]; $paddedBits = str_pad($bits, 64, $signBit, STR_PAD_LEFT); $restBits = substr($bits, 1); var_dump($bits); var_dump($signBit); var_dump($restBits); var_dump($paddedBits); var_dump(bindec($paddedBits)); var_dump(intval($paddedBits, 2)); var_dump(bindec2($paddedBits)); $paddedHexs = str_pad($d, 8, $signBit === '0' ? "\x00" : "\xFF", STR_PAD_LEFT); // 保持decbin()输出的大端序octet var_dump(bin2hex($paddedHexs)); var_dump(unpack('q', $paddedHexs)); // signed long long (always 64 bit, machine byte order) var_dump(unpack('Q', $paddedHexs)); // unsigned long long (always 64 bit, machine byte order) var_dump(unpack('J', $paddedHexs)); // unsigned long long (always 64 bit, big endian byte order) var_dump(unpack('P', $paddedHexs)); // unsigned long long (always 64 bit, little endian byte order) $paddedHexs = str_pad(strrev($d), 8, $signBit === '0' ? "\x00" : "\xFF", STR_PAD_RIGHT); // 大端序转小端序octet var_dump(bin2hex($paddedHexs)); var_dump(unpack('q', $paddedHexs)); // signed long long (always 64 bit, machine byte order) var_dump(unpack('Q', $paddedHexs)); // unsigned long long (always 64 bit, machine byte order) var_dump(unpack('J', $paddedHexs)); // unsigned long long (always 64 bit, big endian byte order) var_dump(unpack('P', $paddedHexs)); // unsigned long long (always 64 bit, little endian byte order)
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 34
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 136, Position 2 = 138
Branch analysis from position: 136
1 jumps found. (Code = 42) Position 1 = 139
Branch analysis from position: 139
2 jumps found. (Code = 43) Position 1 = 185, Position 2 = 187
Branch analysis from position: 185
1 jumps found. (Code = 42) Position 1 = 188
Branch analysis from position: 188
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 187
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 138
2 jumps found. (Code = 43) Position 1 = 185, Position 2 = 187
Branch analysis from position: 185
Branch analysis from position: 187
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 136, Position 2 = 138
Branch analysis from position: 136
Branch analysis from position: 138
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 34
Branch analysis from position: 25
Branch analysis from position: 34
filename:       /in/5Xj4i
function name:  (null)
number of ops:  227
compiled vars:  !0 = $i, !1 = $trimmedPadding, !2 = $signBitsPading, !3 = $o, !4 = $d, !5 = $bits, !6 = $signBit, !7 = $paddedBits, !8 = $restBits, !9 = $paddedHexs
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   ASSIGN                                                   !0, -114514
   29     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'decbin'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $11     
          5        SEND_VAR                                                 $11
          6        DO_ICALL                                                 
   30     7        INIT_FCALL                                               'ltrim'
          8        INIT_FCALL                                               'decbin'
          9        SEND_VAR                                                 !0
         10        DO_ICALL                                         $13     
         11        SEND_VAR                                                 $13
         12        IS_SMALLER                                               !0, 0
         13      > JMPZ                                                     ~14, ->16
         14    >   QM_ASSIGN                                        ~15     '1'
         15      > JMP                                                      ->17
         16    >   QM_ASSIGN                                        ~15     '0'
         17    >   SEND_VAL                                                 ~15
         18        DO_ICALL                                         $16     
         19        ASSIGN                                                   !1, $16
   31    20        INIT_FCALL                                               'var_dump'
         21        SEND_VAR                                                 !1
         22        DO_ICALL                                                 
   32    23        IS_SMALLER                                               !0, 0
         24      > JMPZ                                                     ~19, ->34
   33    25    >   INIT_FCALL                                               'str_repeat'
         26        SEND_VAL                                                 '1'
         27        STRLEN                                           ~20     !1
         28        MOD                                              ~21     ~20, 8
         29        SUB                                              ~22     8, ~21
         30        SEND_VAL                                                 ~22
         31        DO_ICALL                                         $23     
         32        QM_ASSIGN                                        ~24     $23
         33      > JMP                                                      ->42
   34    34    >   INIT_FCALL                                               'str_repeat'
         35        SEND_VAL                                                 '0'
         36        STRLEN                                           ~25     !1
         37        MOD                                              ~26     ~25, 8
         38        SUB                                              ~27     8, ~26
         39        SEND_VAL                                                 ~27
         40        DO_ICALL                                         $28     
         41        QM_ASSIGN                                        ~24     $28
   32    42    >   ASSIGN                                                   !2, ~24
   35    43        INIT_FCALL                                               'var_dump'
         44        CONCAT                                           ~30     !2, !1
         45        SEND_VAL                                                 ~30
         46        DO_ICALL                                                 
   36    47        INIT_FCALL                                               'var_dump'
         48        INIT_FCALL                                               'bin2hex'
         49        INIT_FCALL                                               'bin2ascii'
         50        CONCAT                                           ~32     !2, !1
         51        SEND_VAL                                                 ~32
         52        DO_FCALL                                      0  $33     
         53        SEND_VAR                                                 $33
         54        DO_ICALL                                         $34     
         55        SEND_VAR                                                 $34
         56        DO_ICALL                                                 
   37    57        INIT_FCALL                                               'rtrim'
         58        INIT_FCALL                                               'base64_encode'
         59        INIT_FCALL                                               'bin2ascii'
         60        CONCAT                                           ~36     !2, !1
         61        SEND_VAL                                                 ~36
         62        DO_FCALL                                      0  $37     
         63        SEND_VAR                                                 $37
         64        DO_ICALL                                         $38     
         65        SEND_VAR                                                 $38
         66        SEND_VAL                                                 '%3D'
         67        DO_ICALL                                         $39     
         68        ASSIGN                                                   !3, $39
   38    69        INIT_FCALL                                               'var_dump'
         70        SEND_VAR                                                 !3
         71        DO_ICALL                                                 
   40    72        INIT_FCALL                                               'base64_decode'
         73        SEND_VAR                                                 !3
         74        DO_ICALL                                         $42     
         75        ASSIGN                                                   !4, $42
   41    76        INIT_FCALL                                               'var_dump'
         77        INIT_FCALL                                               'bin2hex'
         78        SEND_VAR                                                 !4
         79        DO_ICALL                                         $44     
         80        SEND_VAR                                                 $44
         81        DO_ICALL                                                 
   42    82        INIT_FCALL                                               'ascii2bin'
         83        SEND_VAR                                                 !4
         84        DO_FCALL                                      0  $46     
         85        ASSIGN                                                   !5, $46
   43    86        FETCH_DIM_R                                      ~48     !5, 0
         87        ASSIGN                                                   !6, ~48
   44    88        INIT_FCALL                                               'str_pad'
         89        SEND_VAR                                                 !5
         90        SEND_VAL                                                 64
         91        SEND_VAR                                                 !6
         92        SEND_VAL                                                 0
         93        DO_ICALL                                         $50     
         94        ASSIGN                                                   !7, $50
   45    95        INIT_FCALL                                               'substr'
         96        SEND_VAR                                                 !5
         97        SEND_VAL                                                 1
         98        DO_ICALL                                         $52     
         99        ASSIGN                                                   !8, $52
   46   100        INIT_FCALL                                               'var_dump'
        101        SEND_VAR                                                 !5
        102        DO_ICALL                                                 
   47   103        INIT_FCALL                                               'var_dump'
        104        SEND_VAR                                                 !6
        105        DO_ICALL                                                 
   48   106        INIT_FCALL                                               'var_dump'
        107        SEND_VAR                                                 !8
        108        DO_ICALL                                                 
   49   109        INIT_FCALL                                               'var_dump'
        110        SEND_VAR                                                 !7
        111        DO_ICALL                                                 
   50   112        INIT_FCALL                                               'var_dump'
        113        INIT_FCALL                                               'bindec'
        114        SEND_VAR                                                 !7
        115        DO_ICALL                                         $58     
        116        SEND_VAR                                                 $58
        117        DO_ICALL                                                 
   51   118        INIT_FCALL                                               'var_dump'
        119        INIT_FCALL                                               'intval'
        120        SEND_VAR                                                 !7
        121        SEND_VAL                                                 2
        122        DO_ICALL                                         $60     
        123        SEND_VAR                                                 $60
        124        DO_ICALL                                                 
   52   125        INIT_FCALL                                               'var_dump'
        126        INIT_FCALL                                               'bindec2'
        127        SEND_VAR                                                 !7
        128        DO_FCALL                                      0  $62     
        129        SEND_VAR                                                 $62
        130        DO_ICALL                                                 
   54   131        INIT_FCALL                                               'str_pad'
        132        SEND_VAR                                                 !4
        133        SEND_VAL                                                 8
        134        IS_IDENTICAL                                             !6, '0'
        135      > JMPZ                                                     ~64, ->138
        136    >   QM_ASSIGN                                        ~65     '%00'
        137      > JMP                                                      ->139
        138    >   QM_ASSIGN                                        ~65     '%FF'
        139    >   SEND_VAL                                                 ~65
        140        SEND_VAL                                                 0
        141        DO_ICALL                                         $66     
        142        ASSIGN                                                   !9, $66
   55   143        INIT_FCALL                                               'var_dump'
        144        INIT_FCALL                                               'bin2hex'
        145        SEND_VAR                                                 !9
        146        DO_ICALL                                         $68     
        147        SEND_VAR                                                 $68
        148        DO_ICALL                                                 
   56   149        INIT_FCALL                                               'var_dump'
        150        INIT_FCALL                                               'unpack'
        151        SEND_VAL                                                 'q'
        152        SEND_VAR                                                 !9
        153        DO_ICALL                                         $70     
        154        SEND_VAR                                                 $70
        155        DO_ICALL                                                 
   57   156        INIT_FCALL                                               'var_dump'
        157        INIT_FCALL                                               'unpack'
        158        SEND_VAL                                                 'Q'
        159        SEND_VAR                                                 !9
        160        DO_ICALL                                         $72     
        161        SEND_VAR                                                 $72
        162        DO_ICALL                                                 
   58   163        INIT_FCALL                                               'var_dump'
        164        INIT_FCALL                                               'unpack'
        165        SEND_VAL                                                 'J'
        166        SEND_VAR                                                 !9
        167        DO_ICALL                                         $74     
        168        SEND_VAR                                                 $74
        169        DO_ICALL                                                 
   59   170        INIT_FCALL                                               'var_dump'
        171        INIT_FCALL                                               'unpack'
        172        SEND_VAL                                                 'P'
        173        SEND_VAR                                                 !9
        174        DO_ICALL                                         $76     
        175        SEND_VAR                                                 $76
        176        DO_ICALL                                                 
   60   177        INIT_FCALL                                               'str_pad'
        178        INIT_FCALL                                               'strrev'
        179        SEND_VAR                                                 !4
        180        DO_ICALL                                         $78     
        181        SEND_VAR                                                 $78
        182        SEND_VAL                                                 8
        183        IS_IDENTICAL                                             !6, '0'
        184      > JMPZ                                                     ~79, ->187
        185    >   QM_ASSIGN                                        ~80     '%00'
        186      > JMP                                                      ->188
        187    >   QM_ASSIGN                                        ~80     '%FF'
        188    >   SEND_VAL                                                 ~80
        189        SEND_VAL                                                 1
        190        DO_ICALL                                         $81     
        191        ASSIGN                                                   !9, $81
   61   192        INIT_FCALL                                               'var_dump'
        193        INIT_FCALL                                               'bin2hex'
        194        SEND_VAR                                                 !9
        195        DO_ICALL                                         $83     
        196        SEND_VAR                                                 $83
        197        DO_ICALL                                                 
   62   198        INIT_FCALL                                               'var_dump'
        199        INIT_FCALL                                               'unpack'
        200        SEND_VAL                                                 'q'
        201        SEND_VAR                                                 !9
        202        DO_ICALL                                         $85     
        203        SEND_VAR                                                 $85
        204        DO_ICALL                                                 
   63   205        INIT_FCALL                                               'var_dump'
        206        INIT_FCALL                                               'unpack'
        207        SEND_VAL                                                 'Q'
        208        SEND_VAR                                                 !9
        209        DO_ICALL                                         $87     
        210        SEND_VAR                                                 $87
        211        DO_ICALL                                                 
   64   212        INIT_FCALL                                               'var_dump'
        213        INIT_FCALL                                               'unpack'
        214        SEND_VAL                                                 'J'
        215        SEND_VAR                                                 !9
        216        DO_ICALL                                         $89     
        217        SEND_VAR                                                 $89
        218        DO_ICALL                                                 
   65   219        INIT_FCALL                                               'var_dump'
        220        INIT_FCALL                                               'unpack'
        221        SEND_VAL                                                 'P'
        222        SEND_VAR                                                 !9
        223        DO_ICALL                                         $91     
        224        SEND_VAR                                                 $91
        225        DO_ICALL                                                 
        226      > RETURN                                                   1

Function bin2ascii:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 4
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 4
Branch analysis from position: 21
Branch analysis from position: 4
filename:       /in/5Xj4i
function name:  bin2ascii
number of ops:  23
compiled vars:  !0 = $input, !1 = $output, !2 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        ASSIGN                                                   !1, ''
    5     2        ASSIGN                                                   !2, 0
          3      > JMP                                                      ->18
    6     4    >   INIT_FCALL                                               'chr'
          5        INIT_FCALL                                               'intval'
          6        INIT_FCALL                                               'substr'
          7        SEND_VAR                                                 !0
          8        SEND_VAR                                                 !2
          9        SEND_VAL                                                 8
         10        DO_ICALL                                         $5      
         11        SEND_VAR                                                 $5
         12        SEND_VAL                                                 2
         13        DO_ICALL                                         $6      
         14        SEND_VAR                                                 $6
         15        DO_ICALL                                         $7      
         16        ASSIGN_OP                                     8          !1, $7
    5    17        ASSIGN_OP                                     1          !2, 8
         18    >   STRLEN                                           ~10     !0
         19        IS_SMALLER                                               !2, ~10
         20      > JMPNZ                                                    ~11, ->4
    8    21    > > RETURN                                                   !1
    9    22*     > RETURN                                                   null

End of function bin2ascii

Function ascii2bin:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Xj4i
function name:  ascii2bin
number of ops:  11
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   13     1        INIT_FCALL                                               'array_reduce'
          2        INIT_FCALL                                               'str_split'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5        SEND_VAR                                                 $1
          6        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          7        SEND_VAL                                                 ~2
          8        DO_ICALL                                         $3      
          9      > RETURN                                                   $3
   14    10*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Xj4i
function name:  {closure}
number of ops:  17
compiled vars:  !0 = $acc, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        INIT_FCALL                                               'str_pad'
          3        INIT_FCALL                                               'decbin'
          4        INIT_FCALL                                               'ord'
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $2      
          7        SEND_VAR                                                 $2
          8        DO_ICALL                                         $3      
          9        SEND_VAR                                                 $3
         10        SEND_VAL                                                 8
         11        SEND_VAL                                                 '0'
         12        SEND_VAL                                                 0
         13        DO_ICALL                                         $4      
         14        ASSIGN_OP                                     8  ~5      !0, $4
         15      > RETURN                                                   ~5
         16*     > RETURN                                                   null

End of Dynamic Function 0

End of function ascii2bin

Function bindec2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 27
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 10
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 10
Branch analysis from position: 21
Branch analysis from position: 10
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 10
Branch analysis from position: 21
Branch analysis from position: 10
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/5Xj4i
function name:  bindec2
number of ops:  32
compiled vars:  !0 = $bin, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
   18     1        STRLEN                                           ~2      !0
          2        IS_EQUAL                                         ~3      ~2, 64
          3      > JMPZ_EX                                          ~3      ~3, ->7
          4    >   FETCH_DIM_R                                      ~4      !0, 0
          5        IS_EQUAL                                         ~5      ~4, '1'
          6        BOOL                                             ~3      ~5
          7    > > JMPZ                                                     ~3, ->27
   19     8    >   ASSIGN                                                   !1, 0
          9      > JMP                                                      ->19
   20    10    >   FETCH_DIM_R                                      ~8      !0, !1
         11        IS_EQUAL                                                 ~8, '1'
         12      > JMPZ                                                     ~9, ->15
         13    >   QM_ASSIGN                                        ~10     '0'
         14      > JMP                                                      ->16
         15    >   QM_ASSIGN                                        ~10     '1'
         16    >   ASSIGN_DIM                                               !0, !1
         17        OP_DATA                                                  ~10
   19    18        PRE_INC                                                  !1
         19    >   IS_SMALLER                                               !1, 64
         20      > JMPNZ                                                    ~12, ->10
   23    21    >   INIT_FCALL                                               'bindec'
         22        SEND_VAR                                                 !0
         23        DO_ICALL                                         $13     
         24        ADD                                              ~14     $13, 1
         25        MUL                                              ~15     ~14, -1
         26      > RETURN                                                   ~15
   25    27    >   INIT_FCALL                                               'bindec'
         28        SEND_VAR                                                 !0
         29        DO_ICALL                                         $16     
         30      > RETURN                                                   $16
   26    31*     > RETURN                                                   null

End of function bindec2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.62 ms | 1485 KiB | 35 Q