1
2
3
4
5
6
7
8
9
10
11
12
13
14
sub decode_d2b {
my $lb = $_[1];
my $hb = $_[0];
my $z = hex($hb)*256 + hex($lb);
if (($z & 0x8000) == 0x8000) {
my $y = -((~hex($hb) & 255) + ((~hex($lb) & 255) + 1)/256);
return $y;
}
else {
my $y = hex($hb) + ((hex($lb))/256);
return $y;
}
return;
}
2012-09-17T09:15:42 JuMi2006Die Doku sagt:
$lb $hb -> $y
00 00 -> 0
00 01 -> 1/256
FF FF -> -1/256
FF F0 -> -1
80 00 -> -128
80 01 -> -127,996
7F FF -> 127,996
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
use strict; use warnings; while (<DATA>) { my ($hb, $lb) = split /\s+/; print "$hb $lb -> " . decode_d2b($hb, $lb) . "\n"; } sub decode_d2b { my $hb = hex($_[0]); my $lb = hex($_[1]); if ($hb & 0x80) { return -( (~$hb & 255) + ((~$lb & 255) + 1)/256 ); } else { return $hb + $lb/256; } } __DATA__ 00 00 -> 0 00 01 -> 1/256 FF FF -> -1/256 FF F0 -> -1 80 00 -> -128 80 01 -> -127,996 7F FF -> 127,996
1
2
3
4
5
6
7
00 00 -> 0
00 01 -> 0.00390625
FF FF -> -0.00390625
FF F0 -> -0.0625
80 00 -> -128
80 01 -> -127.99609375
7F FF -> 127.99609375
1 2 3 4 5 6 7 8
sub encode_b2d { my $y = shift; $y *= 256; $y = $y & 0xffff if ($y < 0); my $hb = int $y/256; my $lb = $y % 256; return (sprintf("%0.2X", $hb), sprintf("%0.2X", $lb)); }
sub decode_d2b { return unpack("s", pack("s", hex($_[0].$_[1]))) / 256; }
my ($lb, $hb) = unpack("H[2]H[2]", pack("s", $num * 256));