1007.protobuf序列化string数据遇'\0'截断问题

探讨了在使用Protobuf进行对象序列化时遇到的特殊问题,即序列化后的字符串中包含''字符,导致字符串大小计算不准确,进而影响反序列化的正确性。文章提供了具体的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 背景:

      使用protobuf在进行序列化的过程 中碰到一个,比较恶心的问题,将xx   对象序列化为 str 时,反序列化,得不到正确的结果。 问题具有一定的偶然性,和xx对象的数据有关,调试好久,才发现问题所在。

 解决方案:

         sMainFrame 是序列化之后的string对象,大小为sMainFrame.size(); 此时 根据数据的不同, 进行测试会出现 sMainFrame.size() 与 strlen(sMainFrame.c_str()) 大小不一致,我还 以为是序列化不成功,其列化是成功的,不过string中有'\0'字符,这样比较是不合理的。

        应该在反序列化处进行处理。具体方法如下:

 

While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either that the input has been truncated or that an embedded message misreported its own length. 2025-06-12 21:47:02,722 [12] ERROR PitayaClient.Zmq.ZeroMqClient - at Google.Protobuf.CodedInputStream.RefillBuffer(Boolean mustSucceed) in T:\src\github\protobuf\csharp\src\Google.Protobuf\CodedInputStream.cs:line 1426 at Google.Protobuf.CodedInputStream.ReadRawByte() in T:\src\github\protobuf\csharp\src\Google.Protobuf\CodedInputStream.cs:line 1476 at Google.Protobuf.CodedInputStream.ReadRawVarint64() in T:\src\github\protobuf\csharp\src\Google.Protobuf\CodedInputStream.cs:line 1205 at Google.Protobuf.UnknownFieldSet.MergeFieldFrom(CodedInputStream input) in T:\src\github\protobuf\csharp\src\Google.Protobuf\UnknownFieldSet.cs:line 190 at Google.Protobuf.UnknownFieldSet.MergeFieldFrom(UnknownFieldSet unknownFields, CodedInputStream input) in T:\src\github\protobuf\csharp\src\Google.Protobuf\UnknownFieldSet.cs:line 269 at FCoreProto4GDR.PosiItem.MergeFrom(CodedInputStream input) in E:\GUI\ArdaClient\protobufLib\ProtoGenerated\Fcore4GDR.cs:line 4431 at Google.Protobuf.CodedInputStream.ReadMessage(IMessage builder) in T:\src\github\protobuf\csharp\src\Google.Protobuf\CodedInputStream.cs:line 621 at FCoreProto4GDR.PubDetails.MergeFrom(CodedInputStream input) in E:\GUI\ArdaClient\protobufLib\ProtoGenerated\Fcore4GDR.cs:line 9135 at Google.Protobuf.MessageExtensions.MergeFrom(IMessage message, Byte[] data, Boolean discardUnknownFields, ExtensionRegistry registry) in T:\src\github\protobuf\csharp\src\Google.Protobuf\MessageExtensions.cs:line 214 at Google.Protobuf.MessageParser`1.ParseFrom(Byte[] data) in T:\src\github\protobuf\csharp\src\Google.Protobuf\MessageParser.cs:line 247 at PitayaClient.Zmq.ZeroMqClient.SubscribeOrdRelated() in D:\project\GUI\ardaclient\PitayaClient\Zmq\ZeroMqClient.cs:line 37
最新发布
06-14
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

guangshui516

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值