Thanks for ur help....when i used the format specified by you i
observed a strange behaviour..i have given $a value as 271 ans i
expect that the data while sending through socket would be 00 00 01 0f
that is hex value(271) 0x010F but what i saw as a output of my socket
is data as 32 37 31...since i tried with L option as well both are
giving the same result.how to send that value as a hex itself..
Okay, if you gave $a a value of 271, then the data you want to send
through the socket would be 00 00 01 0f (hexadecimal) BUT ONLY IF the
data is in big-endian order. If the platforms are little-endian, then
217 would be sent as 0f 01 00 00 (hexadecimal). Just keep this in
mind. (Most platforms I work on are little-endian, so it's good to be
aware of the ordering.)
You said that when you tried sending 271 through the socket, the
socket received the bytes 32 37 31 (hexadecimal) instead of what you
expected of 00 00 01 0f (hexadecimal). Well, the values 32, 37, 31
(hexadecimal) correspond to the values 50, 55, 49 (decimal) which
correspond to the ASCII characters '2', '7', '1'.
It looks like when you packed the value 271 you mistakenly packed
it as a string (with something like "Z*") and not as an integer (with
"I"). Go back into your perl code and make sure that the 271 value is
the first value given after the pack-string in the pack() function,
and that the first character in your pack-string is "I" (and not "Z",
"A", or "a").
for a variable $b i need to send 16 characters iam wondering during
packing what will happen to the Null Character of the string.
Are you trying to pack a null-terminated string in the "char b[16]"
field? If so, the "Z16" specifier will be sure to terminate whatever
perl string you give it with a null-byte. For example, if you write:
my $string = pack("Z5", "abcdefgh");
then $string will be set to "abcd\0". (Notice that it doesn't use any
more characters that you told it to (in this case, 5) and that the
resulting string is always null-terminated, even if it means
truncating the original string.)
Whether or not you need that string to be null-terminated when you
send it through the socket depends on the receiving program. If it
needs to be null-terminated, use "Z16". If it doesn't need to be null-
terminated, you might want to use "a16". If it's not a string at all
but rather sixteen different integer values, use "c16". This is just
a guess, but most likely you'll want to use "Z16" (but I'll never be
sure unless I can read how the receiving program handles the data it
gets).
Is is possible to post the hex output of a sample structure that
you are trying to match? (Just one would be good; any more would
probably be too much.) That way a lot of questions can get answered
and I can help you better.
By the way, if you're confused about how to use the pack()
function, you may consider reading the Perl tutorial on "pack" and
"unpack" by typing "perldoc perlpacktut" at the DOS/Unix prompt. (I
found this page after I used pack() and unpack() extensively, and it
still taught me a lot.)
I hope this helps, Rams.
-- Jean-Luc Romano