0

我正在尝试在 php 中实现一个解析器,它可以读取各种大小的二进制文件(例如 $fsize)。在这些文件中有重复的 16 字节序列,我想独立处理(一次一个 16 字节序列)。我设法以以下形式读取和隔离每个这样的 16 字节序列:

处理样本[2]...

打印 16 个样本字节

$bit_sequence='100000000101110010000000011001110110101101100111101001101010001001110111001111110001000000000000101000110100101010010101011

大批:

$bits=( [0] => 1 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] = > 0 [8] => 0 [9] => 1 [10] => 0 [11] => 1 [12] => 1 [13] => 1 [14] => 0 [15] => 0 [16] => 1 [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0 [23] => 0 [24 ] => 0 [25] => 1 [26] => 1 [27] => 0 [28] => 0 [29] => 1 [30] => 1 [31] => 1 [32] = > 0 [33] => 1 [34] => 1 [35] => 0 [36] => 1 [37] => 0 [38] => 1 [39] => 1 [40] => 0 [41] => 1 [42] => 1 [43] => 0 [44] => 0 [45] => 1 [46] => 1 [47] => 1 [48] => 1 [49 ] => 0 [50] => 1 [51] => 0 [52] => 0 [53] => 1 [54] => 1 [55] => 0 [56] => 1 [57] = > 0 [58] => 1 [59] => 0 [60] => 0 [61] => 0 [62] => 1 [63] => 0 [64] => 0 [65] =>1 [66] => 1 [67] => 1 [68] => 0 [69] => 1 [70] => 1 [71] => 1 [72] => 0 [73] => 0 [ 74] => 1 [75] => 1 [76] => 1 [77] => 1 [78] => 1 [79] => 1 [80] => 0 [81] => 0 [82] => 0 [83] => 1 [84] => 0 [85] => 0 [86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 1 [97] => 0 [98] => 1 [ 99] => 0 [100] => 0 [101] => 0 [102] => 1 [103] => 1 [104] => 0 [105] => 1 [106] => 0 [107] => 0 [108] => 1 [109] => 0 [110] => 1 [111] => 0 [112] => 1 [113] => 0 [114] => 0 [115] => 1 [116] => 1 [117] => 0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [ 124] => 1 [125] => 0 [126] => 0 [127] => 0)1 [68] => 0 [69] => 1 [70] => 1 [71] => 1 [72] => 0 [73] => 0 [74] => 1 [75] => 1 [ 76] => 1 [77] => 1 [78] => 1 [79] => 1 [80] => 0 [81] => 0 [82] => 0 [83] => 1 [84] => 0 [85] => 0 [86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 1 [97] => 0 [98] => 1 [99] => 0 [100] => 0 [ 101] => 0 [102] => 1 [103] => 1 [104] => 0 [105] => 1 [106] => 0 [107] => 0 [108] => 1 [109] => 0 [110] => 1 [111] => 0 [112] => 1 [113] => 0 [114] => 0 [115] => 1 [116] => 1 [117] => 0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [124] => 1 [125] => 0 [ 126] => 0 [127] => 0)1 [68] => 0 [69] => 1 [70] => 1 [71] => 1 [72] => 0 [73] => 0 [74] => 1 [75] => 1 [ 76] => 1 [77] => 1 [78] => 1 [79] => 1 [80] => 0 [81] => 0 [82] => 0 [83] => 1 [84] => 0 [85] => 0 [86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 1 [97] => 0 [98] => 1 [99] => 0 [100] => 0 [ 101] => 0 [102] => 1 [103] => 1 [104] => 0 [105] => 1 [106] => 0 [107] => 0 [108] => 1 [109] => 0 [110] => 1 [111] => 0 [112] => 1 [113] => 0 [114] => 0 [115] => 1 [116] => 1 [117] => 0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [124] => 1 [125] => 0 [ 126] => 0 [127] => 0)1 [71] => 1 [72] => 0 [73] => 0 [74] => 1 [75] => 1 [76] => 1 [77] => 1 [78] => 1 [ 79] => 1 [80] => 0 [81] => 0 [82] => 0 [83] => 1 [84] => 0 [85] => 0 [86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 1 [97] => 0 [98] => 1 [99] => 0 [100] => 0 [101] => 0 [102] => 1 [103] => 1 [ 104] => 0 [105] => 1 [106] => 0 [107] => 0 [108] => 1 [109] => 0 [110] => 1 [111] => 0 [112] => 1 [113] => 0 [114] => 0 [115] => 1 [116] => 1 [117] => 0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [124] => 1 [125] => 0 [126] => 0 [127] => 0)1 [71] => 1 [72] => 0 [73] => 0 [74] => 1 [75] => 1 [76] => 1 [77] => 1 [78] => 1 [ 79] => 1 [80] => 0 [81] => 0 [82] => 0 [83] => 1 [84] => 0 [85] => 0 [86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 1 [97] => 0 [98] => 1 [99] => 0 [100] => 0 [101] => 0 [102] => 1 [103] => 1 [ 104] => 0 [105] => 1 [106] => 0 [107] => 0 [108] => 1 [109] => 0 [110] => 1 [111] => 0 [112] => 1 [113] => 0 [114] => 0 [115] => 1 [116] => 1 [117] => 0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [124] => 1 [125] => 0 [126] => 0 [127] => 0)1 [78] => 1 [79] => 1 [80] => 0 [81] => 0 [82] => 0 [83] => 1 [84] => 0 [85] => 0 [ 86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 1 [97] => 0 [98] => 1 [99] => 0 [100] => 0 [101] => 0 [102] => 1 [103] => 1 [104] => 0 [105] => 1 [106] => 0 [107] => 0 [108] => 1 [109] => 0 [110] => 1 [ 111] => 0 [112] => 1 [113] => 0 [114] => 0 [115] => 1 [116] => 1 [117] => 0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [124] => 1 [125] => 0 [126] => 0 [127] => 0)1 [78] => 1 [79] => 1 [80] => 0 [81] => 0 [82] => 0 [83] => 1 [84] => 0 [85] => 0 [ 86] => 0 [87] => 0 [88] => 0 [89] => 0 [90] => 0 [91] => 0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 1 [97] => 0 [98] => 1 [99] => 0 [100] => 0 [101] => 0 [102] => 1 [103] => 1 [104] => 0 [105] => 1 [106] => 0 [107] => 0 [108] => 1 [109] => 0 [110] => 1 [ 111] => 0 [112] => 1 [113] => 0 [114] => 0 [115] => 1 [116] => 1 [117] => 0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [124] => 1 [125] => 0 [126] => 0 [127] => 0)0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 1 [97] => 0 [98] => 1 [99] => 0 [ 100] => 0 [101] => 0 [102] => 1 [103] => 1 [104] => 0 [105] => 1 [106] => 0 [107] => 0 [108] => 1 [109] => 0 [110] => 1 [111] => 0 [112] => 1 [113] => 0 [114] => 0 [115] => 1 [116] => 1 [117] => 0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [124] => 1 [ 125] => 0 [126] => 0 [127] => 0)0 [92] => 0 [93] => 0 [94] => 0 [95] => 0 [96] => 1 [97] => 0 [98] => 1 [99] => 0 [ 100] => 0 [101] => 0 [102] => 1 [103] => 1 [104] => 0 [105] => 1 [106] => 0 [107] => 0 [108] => 1 [109] => 0 [110] => 1 [111] => 0 [112] => 1 [113] => 0 [114] => 0 [115] => 1 [116] => 1 [117] => 0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [124] => 1 [ 125] => 0 [126] => 0 [127] => 0)0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [124] => 1 [125] => 0 [ 126] => 0 [127] => 0)0 [118] => 1 [119] => 1 [120] => 1 [121] => 1 [122] => 1 [123] => 0 [124] => 1 [125] => 0 [ 126] => 0 [127] => 0)

我还有一个数组,其中包含我要存储上述信息的变量...

大批:

$variables=( [0] => wide_avg_txon [1] => wide_avg [2] => wide_peak_rms [3] => wide_peak [4] => low_pw [5] => low_pp ​​[6] => high_pw [7] = > high_pp [8] => battery_voltage [9] => temperature_spr_flag [10] => temperature [11] => alarm_abat [12] => alarm_amem [13] => alarm_atmp [14] => alarm_alck [15] => alarm_prx [16] => alarm_arpb [17] => alarm_awrn [18] => alarm_aalr [19] => mask_mbat [20] => mask_mmem [21] => mask_mtmp [22] => mask_mlck [23] => mask_smx [24 ] => mask_mprb [25] => mask_mwrn [26] => mask_malr [27] => sample_rate [28] => avg_period [29] => months [30] => date_time)

我的目标是找到一种方法来处理整个 128 位序列,就像具有 128 个元素的数组一样,并且可以选择以这种方式提取:

bits[0] -> 存储在变量 $wide_avg_txon (boolean)

bits[1->15] -> 存储在变量 $wide_avg (15-bit unsigned int)

bits[16] -> 存储在变量 $wide_peak_rms (boolean)

bits[17->31] -> 存储在变量 $wide_peak (15-bit unsigned int)

bits[32->39] -> 存储在变量 $low_pw (8-bit unsigned int)

.... (等等)

bits[112->127]-> 存储在变量 $date_time (16-bit unsigned int)

我在 php 和编程方面也很新……

我不知道这是否是最佳解决方案......但我已经设法使用如下代码将表示 1,8 或 16 位二进制序列的字符串存储到变量中:

$wide_avg_txon = substr($bit_sequence,0,1);

//wide_avg_txon: 1

$wide_avg = '0'.substr($bit_sequence,1,15);

//wide_avg: 0000000001100000

$wide_peak_rms = substr($bit_sequence,16,1);

//wide_peak_rms: 1

$wide_peak = '0'.substr($bit_sequence,17,15);

//wide_peak: 0000000001100100

现在我需要以某种方式处理这些字符串(代表二进制数)并转换为十进制数......

对我发布的解决方案的任何建议都会很有用并受到欢迎......

4

2 回答 2

1

事实上,我设法使用一种非常肮脏的方式打破了 128 位序列:

$wide_avg = '0'.substr($bit_sequence,1,15);

$wide_avg_dec=bindec($wide_avg);

回声“wide_avg:”。$wide_avg 。",并以十进制表示:" 。$wide_avg_dec 。"";

等等...

于 2012-08-22T09:55:32.753 回答
0

看来您只是从字符串中创建一个数组:

function getArrayFromString($string)
{
    $length = strlen($string);
    $ret = array();
    for ($i = 0; $i < $length; $i++)
    {
        $ret[] = $string[$i];
    }
}

但是,听起来您正在使用像标志这样的字节序列。这应该像您需要的那样:

define("ADMIN_PRIV", 1); // 1<<1
define("DEV_PRIV", 4); // 1<<3
if ($byte & ADMIN_PRIV)
{
    // this user is admin
}

然而,PHP 的整数只有 32 位长(4 个字节),所以这是您可以使用的最大整数。所以我会按字节工作。

于 2012-08-21T23:37:30.163 回答