Thread Hex to signed Integer (1/256)
(9 answers)
Opened by JuMi2006 at 2012-09-17 11:15
Hi,
Deine Funktion (etwas gestrafft) gibt nicht immer das zurück, was Du angegeben hast (beachte 0xFFF0): Code (perl): (dl
)
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 Ausgabe: Code: (dl
)
1 00 00 -> 0 Editiert von FIFO: Bitmaske auf Zw.-Komplement korrigiert Last edited: 2012-09-17 22:59:34 +0200 (CEST) Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it? -- Brian Kernighan: "The Elements of Programming Style"
|