简体   繁体   English

C# 将序列化对象的字节转换为字符串

[英]C# convert bytes of serialized object to string

I have a problem at the moment that I don't know how to solve.我现在有一个问题,我不知道如何解决。

The technical progress is sending bytes from a TCPClient to a TCPServer.技术进步是从 TCPClient 向 TCPServer 发送字节。 The sent message is as example: 1-[BYTES-OF-SERIALIZED-OBJECT] Now I have to convert these bytes (from a byte array) to a string to work with regex.发送的消息例如: 1-[BYTES-OF-SERIALIZED-OBJECT] 现在我必须将这些字节(从字节数组)转换为字符串以使用正则表达式。

Does anyone have an idea to solve this?有没有人有解决这个问题的想法? Because when I convert the byte array to a string no encoding works to decode.因为当我将字节数组转换为字符串时,没有编码可以解码。 Maybe I have to convert the serialized bytes in the square brackets to something different and then convert it back?也许我必须将方括号中的序列化字节转换为不同的内容,然后再将其转换回来? Any idea?任何的想法?

The solution was to serialize the objects to a base64 encode string.解决方案是将对象序列化为 base64 编码字符串。 The sent message is now as example: (1)[BASE64STRING-OF-SERIALIZED-OBJECT].发送的消息现在作为示例:(1)[BASE64STRING-OF-SERIALIZED-OBJECT]。 Now I can work with regex and don't have to format the base64 string again.现在我可以使用正则表达式,而不必再次格式化 base64 字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM