SharpPcap 编码.UTF8.GetBytes

发布于 2024-09-12 01:25:50 字数 1348 浏览 6 评论 0原文

有人知道获取这些字节中的实际文本的正确方法是什么? 我在这里做错事了。

另一个问题:utf-8 是最通用的编码吗?它可以正确显示大多数字符吗?

    private void device_OnPacketArrival(object sender, SharpPcap.CaptureEventArgs e)
    {
        string str = string.Empty;

        var time = e.Packet.Timeval.Date;
        var len = e.Packet.Data.Length;

        str = "time.Hour: " + time.Hour + " time.Minute: " + time.Minute + " time.Second: " + time.Second + " time.Millisecond: " + time.Millisecond + "len: " + len;
        str += Environment.NewLine + e.Packet.ToString();
        str += Environment.NewLine + " Message: " + BitConverter.ToString(e.Packet.Data);
        //str +=  e.Packet.Data + Environment.NewLine + Environment.NewLine;

        Packet p = Packet.ParsePacket(e.Packet);
        str += e.Packet.Data + Environment.NewLine + Environment.NewLine;

        byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, e.Packet.Data);

        str += Encoding.UTF8.GetBytes(utf8Bytes.ToString()).ToString();
        //txtOutput.Text += "time.Hour: " + time.Hour + "time.Minute: " + time.Minute + "time.Second: " + time.Second + "time.Millisecond:" + time.Millisecond + "len:" + len;
        //txtOutput.Text += e.Packet.ToString();
        //txtOutput.Text += Environment.NewLine;

        WriteToFile(str,null);
       // WriteToFile("",c);

Does someone know which is the right way to get the actual text in these bytes?
I do something wrong here.

And another question: is utf-8 the most generic encoding, that will show most of the chars correctly?

TY

    private void device_OnPacketArrival(object sender, SharpPcap.CaptureEventArgs e)
    {
        string str = string.Empty;

        var time = e.Packet.Timeval.Date;
        var len = e.Packet.Data.Length;

        str = "time.Hour: " + time.Hour + " time.Minute: " + time.Minute + " time.Second: " + time.Second + " time.Millisecond: " + time.Millisecond + "len: " + len;
        str += Environment.NewLine + e.Packet.ToString();
        str += Environment.NewLine + " Message: " + BitConverter.ToString(e.Packet.Data);
        //str +=  e.Packet.Data + Environment.NewLine + Environment.NewLine;

        Packet p = Packet.ParsePacket(e.Packet);
        str += e.Packet.Data + Environment.NewLine + Environment.NewLine;

        byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, e.Packet.Data);

        str += Encoding.UTF8.GetBytes(utf8Bytes.ToString()).ToString();
        //txtOutput.Text += "time.Hour: " + time.Hour + "time.Minute: " + time.Minute + "time.Second: " + time.Second + "time.Millisecond:" + time.Millisecond + "len:" + len;
        //txtOutput.Text += e.Packet.ToString();
        //txtOutput.Text += Environment.NewLine;

        WriteToFile(str,null);
       // WriteToFile("",c);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南冥有猫 2024-09-19 01:25:50

数据包包含二进制数据而不是文本数据。

数据包的某些部分可能包含文本,但您应该只尝试将这些部分转换为文本(而不是整个数据包数据),并且您应该知道文本编码是什么。

不存在“通用”编码。 UTF8 比 ASCII 更通用,因为 ASCII 中的所有文本都将使用 UTF8 进行转换,但通常没有“通用”编码,您应该知道数据的编码是什么。

Packets contain binary data and not textual data.

There can be parts of the packets that contain text but you should only try and translate these parts to text (not the entire packet data) and you should know what is the text encoding.

There is no "generic" encoding. UTF8 is more generic than ASCII in the sense that all the text in ASCII will be converted using UTF8 but generally there is no "generic" encoding and you should know what is the encoding of your data.

北方的韩爷 2024-09-19 01:25:50

如果数据实际上是 UTF-8,那么您要查找的是 Encoding.UTF8.GetString()。

这完全取决于数据字段包含的内容。如果它是另一个协议有效负载,您需要使用 SharpPcap/Packet.Net 解析器解析它,或者,如果特定解析器尚不存在,那么您需要查找协议规范并从中构建您自己的解析器那。

所有传入的数据包在被解析之前都只是一个无意义数据的大字节数组。有时编写解析器很容易,有时可能需要数周(取决于协议的复杂性或已有哪些工具可以解析特定协议)。 SharpPcap/Packet.Net 是一个用于解析数据包数据的相当广泛的协议,但它远未涵盖所有现有的众所周知/使用的协议。

What you're looking for is Encoding.UTF8.GetString() if the data is in fact UTF-8.

It all depends on what the Data field contains. If it's another protocol payload, you'll need to parse it either using a SharpPcap/Packet.Net parser or, if the specific parser doesn't exist yet then you'll need to look up the protocol spec and build your own parser from that.

All incoming packets are nothing but a big byte array of meaningless data until it can be parsed. Sometimes it's easy to write a parser, sometimes it can take many weeks (depending on the protocol's complexity or what tools already exist to parse the specific protocols). SharpPcap/Packet.Net is a pretty extensive protocol for parsing packet data but it's far from covering all of the commonly known/used protocols that exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文