<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
<font face="sans-serif" size="2">On perl 5.10 the pack function allows
you to say pack('n&gt;',$num) to encode a signed integer to big endian.
And pack('n&lt;',$num) to little endian. However this feature is not
available on older version of perl. What is the most elegant means of
achieving this?<br>
<br>
#!/opt/csw/bin/perl</font>
<br>
<font face="sans-serif" size="2">use warnings;</font>
<br>
<font face="sans-serif" size="2">use strict;</font>
<br>
<br>
<font face="sans-serif" size="2">sub encbig4 {</font>
<br>
<font face="sans-serif" size="2">&nbsp; return pack('n&gt;',shift);</font>
<br>
<font face="sans-serif" size="2">}</font>
<br>
<br>
<font face="sans-serif" size="2">sub decbig4 {</font>
<br>
<font face="sans-serif" size="2">&nbsp; return unpack('n&gt;',shift);</font>
<br>
<font face="sans-serif" size="2">}</font>
<br>
<br>
<font face="sans-serif" size="2">my $num=765432987;</font>
<br>
<font face="sans-serif" size="2">unless( $num eq dec4(enc4($num)) ) {</font>
<br>
<font face="sans-serif" size="2">&nbsp; die 'Horribly';</font>
<br>
<font face="sans-serif" size="2">}</font>
</body>
</html>