Parse data received from socket to int - Perl

I have a buffer of data i need to parse as short int (4B).

How do I go about doing this?
jeweeAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
If you mean 4 Bytes,
unpack("N",$buffer)
or
unpack("V",$buffer)
or
unpack("l",$buffer)
or
unpack("L",$buffer)
depending on signedness or endianness
if you mean $buffer = "4B", to be unpacked into 75
you might use
hex $buffer;
or, if there were more of them, each 2 hex digits,
unpack"C*",pack"H*",

see
perldoc -f pack
and
perldoc -f unpack


0
 
manav_mathurCommented:
4 bits is a nibble, short int is 16 bits.
@arr=unpack("S*",$buffer) ;

0
 
ozoCommented:
How was the short int packed into the buffer?
If you really mean 4 bits, you might use
vec($buffer,0,4)
or
map{hex}split//,unpack"H*",$buffer;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.