我在十六进制转储中有数据,但是不知道编码.例如. 0x91 0x05 = 657 [英] I have data in hex dump but don't know the encoding. Eg. 0x91 0x05 = 657

查看:180
本文介绍了我在十六进制转储中有数据,但是不知道编码.例如. 0x91 0x05 = 657的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在hexdump代码中有一些数据. 左手是DEC,右手是十六进制转储代码.

I have some data in hexdump code. left hand are DEC and right hand are hexdump code.

16 = 10
51 = 33
164 = A4 01
388 = 84 03
570 = BA 04
657 = 91 05
1025 = 81 08
246172 = 9C 83 0F

如何计算DEC的十六进制转储? 在perl中,我尝试使用ord()命令,但不起作用.

How to calculate any hexdump to DEC ? In perl, I tried to use ord() command but don't work.

更新 我不知道这叫什么.看起来像7位数据.我尝试在excel中建立公式,如下所示:

Update I don't known what it call. It look like 7bits data. I try to build formula in excel look like these:

DEC = hex2dec(X) + (128^1 * hex2dec(Y-1)) + (128^2 * hex2dec(Z-1)) + ...

推荐答案

您所拥有的是可变长度编码.长度使用哨兵值的形式进行编码:编码数字的每个字节(最后一个字节除外)的高位均已设置.其余的位以低端字节顺序形成数字的二进制补码.

What you have is a variable-length encoding. The length is encoded using a form of sentinel value: Each byte of the encoded number except the last has its high bit set. The remaining bits form the two's-complement encoding of the number in little-ending byte order.

0xxxxxxx                   ⇒                   0xxxxxxx
1xxxxxxx 0yyyyyyy          ⇒          00yyyyyy yxxxxxxx
1xxxxxxx 1yyyyyyy 0zzzzzzz ⇒ 000zzzzz zzyyyyyy yxxxxxxx
etc

以下内容可用于解码流:

The following can be used to decode a stream:

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

这篇关于我在十六进制转储中有数据,但是不知道编码.例如. 0x91 0x05 = 657的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆