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".
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.