use Math::BigInt; $x = Math::BigInt->new(5000000000); print($x->as_hex(), $/); die("This doesn't work on negative numbers.\n") if $x->is_neg(); my $packed = ''; while (!$x->is_zero()) { $y = $x->copy()->bmod(256); $packed = pack('C', $y) . $packed; $x->brsft(8); } print('0x', unpack('H*', $packed), $/); # Verify work.