perl - 我有十六进制转储中的数据,但不知道编码。例如。0x91 0x05 = 657
问题描述
我在 hexdump 代码中有一些数据。左手是 DEC,右手是 hexdump 代码。
16 = 10
51 = 33
164 = A4 01
388 = 84 03
570 = BA 04
657 = 91 05
1025 = 81 08
246172 = 9C 83 0F
如何计算任何 hexdump 到 DEC ?在 perl 中,我尝试使用 ord() 命令但不起作用。
更新 我不知道它叫什么。它看起来像 7 位数据。我尝试在 excel 中构建公式,如下所示:
DEC = hex2dec(X) + (128^1 * hex2dec(Y-1)) + (128^2 * hex2dec(Z-1)) + ...
解决方案
您所拥有的是可变长度编码。长度使用标记值的形式进行编码:编码数字的每个字节(除了最后一个字节)都有其高位设置。其余位以小结尾字节顺序形成数字的二进制补码编码。
0xxxxxxx ⇒ 0xxxxxxx
1xxxxxxx 0yyyyyyy ⇒ 00yyyyyy yxxxxxxx
1xxxxxxx 1yyyyyyy 0zzzzzzz ⇒ 000zzzzz zzyyyyyy yxxxxxxx
etc
以下内容可用于解码流:
use strict;
use warnings;
use feature qw( say );
sub extract_first_num {
$_[0] =~ s/^([\x80-\xFF]*[\x00-\x7F])//
or return;
my $encoded_num = $1;
my $num = 0;
for (reverse unpack 'C*', $encoded_num) {
$num = ( $num << 7 ) | ( $_ & 0x7F );
}
return $num;
}
my $stream_buf = "\x10\x33\xA4\x01\x84\x03\xBA\x04\x91\x05\x81\x08\x9C\x83\x0F";
while ( my ($num) = extract_first_num($stream_buf) ) {
say $num;
}
die("Bad data") if length($stream_buf);
输出:
16
51
164
388
570
657
1025
246172
推荐阅读
- python - 如何让用户根据我创建的字典输入电话号码并让他们以 XXX-XXX-XXXX 格式输入键或值?
- yii - 在 main.php 文件中使用 url::to()
- javascript - 仅向访问特定页面的用户显示内容?
- php - 在不同的选项卡中重用 Blade 模板
- 3d - KinematicBody.move_and_slide() 只能从实例调用
- linux-kernel - UIO驱动——理解实现逻辑
- c++ - C++ 在函数之间传递变量
- node.js - 节点/猫鼬:来自 URL 搜索参数中的数组的 ObjectId
- javascript - 如何根据 selectBox 的值显示或隐藏行?
- spring-boot - 如何在 Docker 中配置 NGINX 以使用在 localhost 上运行的 Spring Boot 应用程序?