in reply to Please Explain this Function
$msgCode = $msgCode.(" " x ($maxMsgCodeLength-length($msgCode)));
That code pads the string to a set width with spaces. The same could be achieved using sprintf or directly with pack using the 'A' format specifier, which pads a string to a set width with spaces. The 'a' specifier actually used in your code would pad with nulls (ASCII 0) but as the string has already been padded to the necessary width no nulls appear. The 'i' specifier will output a signed integer of at least 4 bytes width and the exact format will be architecture dependant. The following might help you to see what is happening.
$ perl -e ' > $maxMsgCodeLength = 15; > $port = 9000; > $ip = q{192.168.106.60}; > $msgCode = q{StartBE}; > $msg = pack qq{A${maxMsgCodeLength}ia*}, $msgCode, $port, $ip; > print $msg;' | hexdump -C 00000000 53 74 61 72 74 42 45 20 20 20 20 20 20 20 20 28 |StartBE + (| 00000010 23 00 00 31 39 32 2e 31 36 38 2e 31 30 36 2e 36 |#..192.16 +8.106.6| 00000020 30 |0| 00000021 $
You can see in this case that the port no of 9000 has been represented by the four hex bytes 28230000 (running under Cygwin on XP). Reversing the process gives:-
$ perl -le 'print unpack q{i}, qq{\x28\x23\x00\x00};' 9000 $
I hope this is useful.
Cheers,
JohnGG
|
---|