#!/usr/bin/perl use strict; use warnings; #assume each value is a perl var masked to lower 8 bits foreach (0x85, 0x05) { my $x = $_; #allow mod of $x in loop my $neg = $x & 0x80; #decide if negative $x = ~$x & 0x7F, $x = -$x if $neg; #adjust if so printf "Input=%2x hex Input=%08b decimal=%d\n", $_, $_, $x; } __END__ Input=85 hex Input=10000101 decimal=-122 Input= 5 hex Input=00000101 decimal=5