我试图在php中实现一个解析器,它读取各种大小的二进制文件(例如$ fsize)。在这些文件中有重复的16字节序列,我想独立处理(一次一个16字节序列)。我已经设法以以下形式读取和隔离每个这样的16字节序列:
处理样本[2] ......
打印16个样本字节
$ bit_sequence = '10000000010111001000000001100111011010110110011110100110101000100111011100111111000100000000000010100011010010101001101111101000'
数组:
$ 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)
我还有一个数组,其中包含我想要存储上述信息的变量...
阵列:
$ 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] =>温度[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个元素的数组,并且可以选择以这样的方式提取:
位[0] - >存储在变量$ wide_avg_txon(boolean)
中位[1-> 15] - >存储在变量$ wide_avg(15位unsigned int)
中位[16] - >存储在变量$ wide_peak_rms(boolean)
中位[17-> 31] - >存储在变量$ wide_peak(15位unsigned int)
中位[32-> 39] - >存储在变量$ low_pw(8位无符号整数)
中....(依此类推)
位[112-> 127] - GT;存储在变量$ date_time(16位无符号整数)
中
我在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
现在我需要以某种方式处理这些字符串(代表二进制数字)并以数字方式转换为十进制数字......
对我发布的解决方案的任何建议都是有用的,欢迎......
答案 0 :(得分:1)
事实上,我设法使用非常脏的方式根据需要打破了128位序列:
$ wide_avg ='0'.substr($ bit_sequence,1,15);
$ wide_avg_dec = bindec($ wide_avg);
echo“wide_avg:”。 $ wide_avg。 “和十进制:”。 $ wide_avg_dec。 “”;
依旧......
答案 1 :(得分:0)
看来你只是从一个字符串中创建一个数组:
function getArrayFromString($string)
{
$length = strlen($string);
$ret = array();
for ($i = 0; $i < $length; $i++)
{
$ret[] = $string[$i];
}
}
然而,听起来你正在使用像flag这样的字节序列。这应该就像你需要的那样:
define("ADMIN_PRIV", 1); // 1<<1
define("DEV_PRIV", 4); // 1<<3
if ($byte & ADMIN_PRIV)
{
// this user is admin
}
但是,PHP的整数只有32位长(4个字节),因此这是您可以使用的最多。所以我会按字节工作。