用PERL处理二进制标量

本文深入探讨了如何使用PERL语言有效地操作和处理二进制标量,包括插入、读取和解析二进制数据,特别是在socket通信中的应用,结合正则表达式的使用,提升PERL在处理二进制流时的效率。
摘要由CSDN通过智能技术生成

 PERL Handling Binary Scalar

1  [Question]:

I adopt select()...sysread()...syswrite() mechanism to handle socket messages, where messages are sysread() into $buffer (binary) before they are syswritten.

Now I want to change two bytes of the message, which denote the length of the whole message. At first, I use following code:

my $msglen=substr($buffer,0,2); # Get the first two bytes
my $declen=hex($msglen);
$declen
+= 3;
substr
($buffer,0,2,$declen); # change the length

However, it doesn't work in this way. If the final value of $declen is 85, then the modified $buffer will be "0x35 0x35 0x00 0x02...". I insert digital number to $buffer but finally got ASCII!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值