But what do you mean "wrongly coded"? How should htonl() be defined?
Your htonl:
sub htonl {
my $input = shift;
my $output = unpack( 'N*', pack( 'L*', $input ) );
return $output;
}
- First, takes the number and "packs it" (converts it to binary) as a signed long:
Which means that the return from that first pack is 4 bytes and encoded in whatever byte-order (endianess) your current platform uses: $packed = pack 'l', 17;;
print length $packed;;
4
print ord( substr $packed, $_, 1 ) for 0 .. 3;;
17
0
0
0
On my intel system, that means little-endian (the low byte comes first).
- But then, you unpack (convert binary to ascii), those 4 bytes, treating them a big-endian long ('N'), resulting in:
$unpacked = unpack 'N', $packed;;
print length( $unpacked );;
9
print ord( substr $unpacked, $_, 1 ) for 0 .. 9;;
50
56
53
50
49
50
54
55
50
0
Resulting in a 9-byte ascii encode string containing the number: 285212672; which is meaningless.
htonl() could be correctly coded as sub htonl{ pack 'l>', $_[0] }; if you see the need for wrapping a built-in function in a silly named wrapper :)
Similarly, you will need to fix your ntohl(); something like this: sub ntohl{ unpack 'l>', $_[0] } would suffice.
That may fix the second part of your problem.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|