使用二进制数据? [英] Working with binary data?

查看:96
本文介绍了使用二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那里的所有大师。我正在编写一个工具,

从网络设备接收二进制数据。数据

以标准格式到达,供应商已记录,例如b $ b。字节0是格式版本,1-4是

0到255之间的整数等等。此时我只是想要将数据格式化为可以的数据阅读

by human并将其写入控制台。我看过一些

的其他例子,说明人们如何才能完成这一点但没有在

visual basic中。我遇到了一个Perl示例

使用解包操作将二进制数据更改为几个字符串
。我已经尝试将数据转换为具有不同类型编码的

字符串。

没有运气,它只是显示为乱码。我已经包含了我的

来源,所以你可以看到我在追求什么。是否有一个可用于Perl解压缩的b $ b b或b / b
hack例程,有人为了使二进制文件使用b $ b数据有用吗?谢谢!


Dan


进口系统

进口System.Net

进口System.Net.Sockets

导入System.Text


公共类NetFlowCollector


私有共享UDPPort为整数= 5000 $ />
私有共享子StartListener()

Dim done As Boolean = False


Dim RemoteIpEndPoint作为新的IPEndPoint (IPAddress.Any,

0)

Dim UDPClient As New UdpClient(UDPPort)


试试

未完成

Dim bytes As Byte()= UDPClient.Receive

(RemoteIpEndPoint)

Console.WriteLine( strData)

结束时


抓住e作为例外

Console.WriteLine(e.ToString())

结束尝试

结束子


公共重载共享功能主要(ByVal args()为

[String] )作为整数

Console.WriteLine("在UDP上收听NetFlow数据

{0} ,UDPPort)

StartListener()


返回0

结束功能''主要

结束类

解决方案

尝试使用BitConverter类。你可以用这种方式打包和解包字节。


" Dan" <一个******* @ discussions.microsoft.com>在消息中写道

news:03 **************************** @ phx.gbl ... < blockquote class =post_quotes>给所有的大师们。我正在编写一个从网络设备接收二进制数据的工具。数据以供应商记录的标准格式到达,例如字节0是格式版本,1-4是0到255之间的整数,等等。此时我只想将数据格式化为人类可以读取的内容。把它写到控制台。我已经看到了一些其他的例子,说明人们如何在视觉基础上完成这一点但没有。我遇到了一个Perl示例
使用解包操作将二进制数据更改为几个字符串。我尝试将数据转换为具有不同类型编码的字符串。没有运气,它只是显示为乱码。我已经包含了我的
来源,所以你可以看到我在追求什么。是否有可用的Perl解压缩操作或某些人为编写二进制数据有用而编写的小程序?谢谢!

Dan

进口系统
进口System.Net
进口System.Net.Sockets
进口System.Text
Public Class NetFlowCollector
私有共享UDPPort为整数= 5000
私有共享子StartListener()
Dim done As Boolean = False

Dim RemoteIpEndPoint作为新的IPEndPoint(IPAddress.Any,
0)
将UDPClient作为新的UdpClient(UDPPort)调暗

尝试
而未完成
Dim bytes As Byte()= UDPClient.Receive
(RemoteIpEndPoint)

Console.WriteLine(strData)
结束时

Catch e作为例外
Console.WriteLine(e.ToString())
结束尝试
结束子

公共重载共享功能Main(ByVal args()As
[String])作为整数
Console.WriteLine(收听UDP上的NetFlow数据{0},UDPPort)
StartListener()

返回0
结束功能''主要结束类



感谢您的回复。这似乎让我朝着正确的方向前进。您能否推荐最有效的方式将这些信息输入一个说

ASCII?所以现在我将信息转储到控制台上,然后我会看到:
见:


00-34-00-00-00 ...


我假设是十六进制,每个组代表一个

字节。我担心,如果我将这些数据转换为

字符串,然后用hex2decimal函数解析它

然后将其提供给ascii,这将太慢。再次感谢




Dan

-----原始信息--- - 尝试使用BitConverter类。你可以用这种方式打包和解包



字节。


这样就可以了。


Dim b As BitConverter

Dim test(5)As Byte


test(0)= 72

test(1)= 69

test(2)= 76

test(3)= 76

test(4)= 79


Dim str As String = Encoding.ASCII.GetString(test,0,test.Length)

Debug.WriteLine(str)

丹 <一个******* @ discussions.microsoft.com>在消息中写道

news:01 **************************** @ phx.gbl ... < blockquote class =post_quotes>感谢您的回复。这似乎让我朝着正确的方向前进。您能否推荐最有效的方式将该信息输入一个说
ASCII?所以现在我将信息转储到控制台,我看到:

00-34-00-00-00 ...

我认为那是十六进制,每组代表一个字节。我担心,如果我将该数据转换为
字符串,然后用hex2decimal函数解析它,然后将其提供给ascii,这将太慢。再次感谢

Dan

-----原帖-----
试试BitConverter类。您可以通过这种方式打包和解压缩


字节。



To all the gurus out there. I am writing a tool that
receives binary data from a network device. The data
arrives in a standard format which the vendor has
documented, e.g. byte 0 is the format version, 1-4 are
integers between 0 and 255, etc. At this point I just
want to format the data into something that can be read
by humans and write it to the console. I have seen some
other examples of how people acomplish this but none in
visual basic. There is a Perl example I ran across that
uses the unpack operation to change the binary data to
several strings. I have tried converting the data to a
string with different types of encoding. No luck with
that it just shows up as jibberish. I have included my
source so you can see what i''m after. Is there a
comperable operation to the Perl unpack or some little
hack routine that someone has written to make the binary
data useful? Thanks!

Dan

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class NetFlowCollector

Private Shared UDPPort As Integer = 5000

Private Shared Sub StartListener()
Dim done As Boolean = False

Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any,
0)
Dim UDPClient As New UdpClient(UDPPort)

Try
While Not done
Dim bytes As Byte() = UDPClient.Receive
(RemoteIpEndPoint)

Console.WriteLine(strData)
End While

Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub

Public Overloads Shared Function Main(ByVal args() As
[String]) As Integer
Console.WriteLine("Listening for NetFlow data on UDP
{0}", UDPPort)
StartListener()

Return 0
End Function ''Main
End Class

解决方案

Try the BitConverter class. You can pack and unpack bytes this way.

"Dan" <an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl...

To all the gurus out there. I am writing a tool that
receives binary data from a network device. The data
arrives in a standard format which the vendor has
documented, e.g. byte 0 is the format version, 1-4 are
integers between 0 and 255, etc. At this point I just
want to format the data into something that can be read
by humans and write it to the console. I have seen some
other examples of how people acomplish this but none in
visual basic. There is a Perl example I ran across that
uses the unpack operation to change the binary data to
several strings. I have tried converting the data to a
string with different types of encoding. No luck with
that it just shows up as jibberish. I have included my
source so you can see what i''m after. Is there a
comperable operation to the Perl unpack or some little
hack routine that someone has written to make the binary
data useful? Thanks!

Dan

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class NetFlowCollector

Private Shared UDPPort As Integer = 5000

Private Shared Sub StartListener()
Dim done As Boolean = False

Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any,
0)
Dim UDPClient As New UdpClient(UDPPort)

Try
While Not done
Dim bytes As Byte() = UDPClient.Receive
(RemoteIpEndPoint)

Console.WriteLine(strData)
End While

Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub

Public Overloads Shared Function Main(ByVal args() As
[String]) As Integer
Console.WriteLine("Listening for NetFlow data on UDP
{0}", UDPPort)
StartListener()

Return 0
End Function ''Main
End Class



Thanks for the reply. That seems to have me moving in
the right direction. Can you recommend the most
efficient way to feed that information "into" an say
ASCII? So right now I dump the info to the console and I
see:

00-34-00-00-00...

I assume that is hex which each group representing one
byte. I''m afraid that if i convert that data into a
string and then parse it out with a hex2decimal function
and then feed it to ascii that will be too slow. Thanks
again!

Dan

-----Original Message-----
Try the BitConverter class. You can pack and unpack


bytes this way.


This will do it.

Dim b As BitConverter
Dim test(5) As Byte

test(0) = 72
test(1) = 69
test(2) = 76
test(3) = 76
test(4) = 79

Dim str As String = Encoding.ASCII.GetString(test, 0, test.Length)
Debug.WriteLine(str)
"Dan" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...

Thanks for the reply. That seems to have me moving in
the right direction. Can you recommend the most
efficient way to feed that information "into" an say
ASCII? So right now I dump the info to the console and I
see:

00-34-00-00-00...

I assume that is hex which each group representing one
byte. I''m afraid that if i convert that data into a
string and then parse it out with a hex2decimal function
and then feed it to ascii that will be too slow. Thanks
again!

Dan

-----Original Message-----
Try the BitConverter class. You can pack and unpack


bytes this way.



这篇关于使用二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆