[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

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?
0
jewee
Asked:
jewee
  • 2
1 Solution
 
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
 
ozoCommented:
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now