二进制读取方法? [英] Binary Read Method?

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

问题描述

您好,


我希望使用Binary Read

方法从文件中提取嵌入的字符串数据。


以下代码示例用于VB.NET中,类似的代码用于

VB6 -


(假设变量声明等)。b​​r />
FileOpen(iFileIn,sInputFile,OpenMode.Binary,OpenAccess.Read)

iRecordEndAddress = iRecordCount * iRecordSize

对于iRecordStartAddress = 1到iRecordEndAddress步骤iRecordSize

FileGet(iFileIn,sData,iRecordStartAddress)

sA = Trim(Strings.Left(sData,8))

sB = Trim(字符串。 Mid(sData,10,60))

..

..

..

sOutPutText& = sA& "," &安培; sB& vbCrLf

下一页

FileClose(iFileIn)

在相同的数据文件中,VB6应用程序在<2秒内完成工作,但是在

VB.NET需要> 15秒。现在我没有涉及两种语言之间围绕性能的问题

,但我想知道其他人建议的最好/最快的表现方式这个任务在VB.NET(2005)下是



我已经尝试过明显的My.Computer.FileSystem.ReadAllBytes和

FileStream方法然而任何可能的速度优势都会丢失

将输入流转换回字符串字符为我的OutPut

文本 - 除非有人能给我一个快速的方法!


任何建议(除了回到VB6)都将不胜感激。


ShaneO


有10种人 - 那些了解Binary和那些

的人不会。

解决方案

我倾向于使用这样的StreamReader:


Dim _sr作为新的StreamReader(sInputFile)


Dim _chars(iRecordSize - 1)作为Char


Dim _text作为新的StringBuilder


虽然sr.Peek()> = 0

_sr.Read(_chars,0,_chars.Length)

_text.AppendFormat(" {0},",(New String(_chars,0,8))。Trim)

_text.AppendFormat(" {0},",(New String(_chars,9,60))。修剪)

...

''对于最后的''字段'',不要附加逗号

_text.AppendFormat(" {0}",(New String(_chars,x,y)) .Trim)

_text.Append(Environment.NewLine)

循环


_sr.Close()


sOutPutText - _text.ToString

你会发现StringBuilder对象上的重复操作远比等效操作更有效
在String对象上。


您还可以创建一个''field''长度数组和一个''field'数组'

起始位置和使用这样的循环:


''确保_ fieldstarts和_fieldlengths长度相同

Dim _fieldstarts As Integer = New Integer(){0,9,...,n}

Dim _fieldlengths As Integer = New Integer (){8,60,...,n}


虽然sr.Peek()> = 0

_sr.Read(_chars,0 ,_chars.Length)

Dim _i作为整数

For _i = 0 To _fieldstarts.Length - 2

_text.AppendFormat(" { 0},",(新字符串(_chars,_fieldstarts(_i),

_fieldlengths(_i)))。修剪)

下一页

当内循环结束时,_i指向最后一个元素

_fieldstarts和_fieldlengths

''对于最后一个''字段'',不要附加逗号,但附加一个cr / lf



_text.AppendFormat(" {0} {1}",(New String(_chars,_fieldstarts(_i),

_fieldlengths(_i)))。修剪,环境。新线)

循环

" ShaneO" < sp **** @ optusnet.com.auwrote in message

news:45 ********************** @ news .optusnet.com.au ...


你好,


我想用文件从文件中提取嵌入的字符串数据二进制读取

方法。


以下代码示例用于VB.NET,类似代码用于

VB6 -


(假设变量声明等)

FileOpen(iFileIn,sInputFile,OpenMode.Binary,OpenAccess.Read)

iRecordEndAddress = iRecordCount * iRecordSize

对于iRecordStartAddress = 1到iRecordEndAddress步骤iRecordSize

FileGet(iFileIn,sData,iRecordStartAddress)

sA = Trim(字符串) .Left(sData,8))

sB = Trim(Strings.Mid(sData,10,60))



。< br $> b $ b。

sOutPutText& = sA& "," &安培; sB& vbCrLf

下一页

FileClose(iFileIn)


在相同的数据文件中,VB6应用程序在< 2秒内完成工作,但是在

VB.NET中它需要> 15秒。现在我没有涉及两种语言之间围绕

性能的问题,但我想知道其他人建议的最好/最快的表现是什么?

VB.NET(2005)下的这样一项任务。


我已经尝试过明显的My.Computer.FileSystem.ReadAllBytes和FileStream
方法但是将

输入流转换回我的OutPut文本的字符串字符会丢失任何可能的速度优势 - 除非

有人可以给我一个快速的方法!


任何建议(除了回到VB6)都将不胜感激。


ShaneO


有10种人 - 那些了解Binary和那些

的人不是。



Stephany Young写道:


我倾向于使用StreamReader是这样的:


Dim _sr作为新的StreamReader(sInputFile)


Dim _chars(iRecordSize - 1)作为Char


Dim _text As New StringBuilder


虽然sr.Peek()> = 0

_sr.Read(_chars,0 ,_chars.Length)

_text.AppendFormat(" {0},",(New String(_chars,0,8))。Trim)

_text。 AppendFormat(" {0},",(New String(_chars,9,60))。修剪)

...

''为最后一个' 'field'',不要附加逗号

_text.AppendFormat(" {0}",(New String(_chars,x,y))。修剪)

_text.Append(Environment.NewLine)

循环


_sr.Close()


sOutPutText - _text.ToString



谢谢Stephany为您服务非常彻底的答案。


我尝试过类似但仍然发现不断循环(大约100K

记录)仍然是破坏性能。但是,我会更准确地按照你的
例子来测试它是否能更快地运行。


与此同时,我再次尝试使用ReadAllBytes

并且发现了一些调整以获得一些速度提升。特别是

中的一个,如你所提到的,String对象在重复操作上效率不高,所以只需删除以下行 -


sOutPutText& = sA& "," &安培; sB& vbCrLf


并将其修改为(已经打开的文件) -


打印(iFileOut,sA&","& sB& vbCrLf)


的整体执行时间减少了惊人的50%! (现在

降低到> 5秒,其他调整)。


你知道一种传递连续字节块的快速方法吗? >
(存储在字节数组中)到字符串中?如果我发现我相信

我将能够提供令人满意的性能,因为这是我目前的

瓶颈。我查看了System.Text.Encoding.Unicode.GetString但是

似乎无法使其正常工作!


ShaneO


有10种人 - 那些了解Binary和那些

的人不会。


< blockquote>很高兴我可以提供帮助。


从你的角度来看,有趣的是找到花时间的b'b



例如:


读取输入文件需要多长时间?


Dim _start As DateTime = DateTime.Now

Dim _sr作为新的StreamReader(sInputFile)

Dim _chars(iRecordSize - 1)作为Char

虽然sr.Peek()> = 0

_sr.Read(_chars,0,_chars.Length)

循环

_sr.Close ()

Console.WriteLine(DateTime.Now.Subtract(_start).Tal talMilliseconds)


然后添加variius''bits''并记下已过去的时间。


你很快就会发现瓶颈所在,可以集中精力去减少这些技术。

" ShaneO" < sp **** @ optusnet.com.auwrote in message

news:45 ********************** @ news .optusnet.com.au ...


Stephany Young写道:


>我会倾向于使用像这样的StreamReader:

Dim _sr作为新的StreamReader(sInputFile)

Dim _chars(iRecordSize - 1)作为Char

Dim _text As New StringBuilder

虽然sr.Peek()> = 0
_sr.Read(_chars,0,_chars.Length)
_text.AppendFormat(" { 0},",(New String(_chars,0,8))。Trim)
_text.AppendFormat(" {0},",(New String(_chars,9,60))。Trim )
...
''对于最后的''字段'',不要附加逗号
_text.AppendFormat(" {0}",(New String(_chars, x,y))。修剪)
_text.Append(Environment.NewLine)
循环

_sr.Close()

sOutPutText - _text。 ToString



Tha nk-you Stephany为你提供了非常彻底的答案。


我尝试过类似但仍然发现不断循环(大约100K

记录)仍然是破坏性能。但是,我会更准确地按照你的
例子来测试它是否能更快地运行。


与此同时,我再次尝试使用ReadAllBytes和

已经找到一些调整来获得一些速度提升。一个特别是

并且如你所提到的,String对象在重复

操作时效率不高,所以只需删除以下行 -


sOutPutText& = sA& "," &安培; sB& vbCrLf


并将其修改为(已经打开的文件) -


打印(iFileOut,sA&","& sB& vbCrLf)


的整体执行时间减少了惊人的50%! (现在

降低到> 5秒,其他调整)。


你知道一种传递连续字节块的快速方法吗? >
(存储在字节数组中)到字符串中?如果我发现我相信

我将能够提供令人满意的性能,因为这是我目前的

瓶颈。我查看了System.Text.Encoding.Unicode.GetString但是

似乎无法使其正常工作!


ShaneO


有10种人 - 那些了解Binary和那些

的人不会。



Hello,

I wish to extract embedded string data from a file using a Binary Read
method.

The following code sample is used in VB.NET and similar code is used in
VB6 -

(Assume variable declarations etc.)
FileOpen(iFileIn, sInputFile, OpenMode.Binary, OpenAccess.Read)
iRecordEndAddress = iRecordCount * iRecordSize
For iRecordStartAddress = 1 To iRecordEndAddress Step iRecordSize
FileGet(iFileIn, sData, iRecordStartAddress)
sA = Trim(Strings.Left(sData, 8))
sB = Trim(Strings.Mid(sData, 10, 60))
..
..
..
sOutPutText &= sA & "," & sB & vbCrLf
Next
FileClose(iFileIn)
On the same datafile the VB6 app does the job in <2 secs, however in
VB.NET it takes >15 secs. Now I''m not getting into the issues
surrounding performance between the two languages, but I would like to
know what others suggest as the best/quickest way to perform such a task
under VB.NET (2005).

I''ve tried the obvious My.Computer.FileSystem.ReadAllBytes and
FileStream methods however any possible speed advantages are lost in
converting the input-stream back into String Characters for my OutPut
Text - unless someone can give me a quick way to do that!

Any suggestions (apart from going back to VB6) would be appreciated.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don''t.

解决方案

I would be inclined to use a StreamReader something like this:

Dim _sr As New StreamReader(sInputFile)

Dim _chars(iRecordSize - 1) as Char

Dim _text As New StringBuilder

While sr.Peek() >= 0
_sr.Read(_chars, 0, _chars.Length)
_text.AppendFormat("{0},", (New String(_chars, 0, 8)).Trim)
_text.AppendFormat("{0},", (New String(_chars, 9, 60)).Trim)
...
'' For the last ''field'', do not append a comma
_text.AppendFormat("{0}", (New String(_chars, x, y)).Trim)
_text.Append(Environment.NewLine)
Loop

_sr.Close()

sOutPutText - _text.ToString

You will find that repeated operations on a StringBuilder object are far
more efficient than the equivalent operations on String objects.

You could also create an array of ''field'' lengths and an array of ''field''
start positions and use those in a loop like this:

'' Make sure that _fieldstarts and _fieldlengths are the same length
Dim _fieldstarts As Integer = New Integer() {0, 9, ... , n}
Dim _fieldlengths As Integer = New Integer() {8, 60, ... , n}

While sr.Peek() >= 0
_sr.Read(_chars, 0, _chars.Length)
Dim _i As Integer
For _i = 0 To _fieldstarts.Length - 2
_text.AppendFormat("{0},", (New String(_chars, _fieldstarts(_i),
_fieldlengths(_i))).Trim)
Next
When the inner loop finishes, _i points to the final element of
_fieldstarts and _fieldlengths
'' For the last ''field'', do not append a comma, but do append a cr/lf
pair
_text.AppendFormat("{0}{1}", (New String(_chars, _fieldstarts(_i),
_fieldlengths(_i))).Trim, Environment.NewLine)
Loop
"ShaneO" <sp****@optusnet.com.auwrote in message
news:45**********************@news.optusnet.com.au ...

Hello,

I wish to extract embedded string data from a file using a Binary Read
method.

The following code sample is used in VB.NET and similar code is used in
VB6 -

(Assume variable declarations etc.)
FileOpen(iFileIn, sInputFile, OpenMode.Binary, OpenAccess.Read)
iRecordEndAddress = iRecordCount * iRecordSize
For iRecordStartAddress = 1 To iRecordEndAddress Step iRecordSize
FileGet(iFileIn, sData, iRecordStartAddress)
sA = Trim(Strings.Left(sData, 8))
sB = Trim(Strings.Mid(sData, 10, 60))
.
.
.
sOutPutText &= sA & "," & sB & vbCrLf
Next
FileClose(iFileIn)
On the same datafile the VB6 app does the job in <2 secs, however in
VB.NET it takes >15 secs. Now I''m not getting into the issues surrounding
performance between the two languages, but I would like to know what
others suggest as the best/quickest way to perform such a task under
VB.NET (2005).

I''ve tried the obvious My.Computer.FileSystem.ReadAllBytes and FileStream
methods however any possible speed advantages are lost in converting the
input-stream back into String Characters for my OutPut Text - unless
someone can give me a quick way to do that!

Any suggestions (apart from going back to VB6) would be appreciated.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don''t.



Stephany Young wrote:

I would be inclined to use a StreamReader something like this:

Dim _sr As New StreamReader(sInputFile)

Dim _chars(iRecordSize - 1) as Char

Dim _text As New StringBuilder

While sr.Peek() >= 0
_sr.Read(_chars, 0, _chars.Length)
_text.AppendFormat("{0},", (New String(_chars, 0, 8)).Trim)
_text.AppendFormat("{0},", (New String(_chars, 9, 60)).Trim)
...
'' For the last ''field'', do not append a comma
_text.AppendFormat("{0}", (New String(_chars, x, y)).Trim)
_text.Append(Environment.NewLine)
Loop

_sr.Close()

sOutPutText - _text.ToString

Thank-you Stephany for your very thorough answer.

I did try similar but still found the constant looping (around 100K
records) was still clobbering performance. I will, however, follow your
example more precisely and test if it works quicker.

In the meantime, I''ve been experimenting again with using ReadAllBytes
and have found some tweaks to gain some speed improvements. One in
particular and as you mentioned, String objects are not too efficient on
repeated operations, so simply removing the following line -

sOutPutText &= sA & "," & sB & vbCrLf

and modifying it to (an already Open File) -

Print(iFileOut, sA & "," & sB & vbCrLf)

has had a staggering 50% reduction in the overall execution time! (Now
down to >5 secs with other tweaks).

Do you know of a quick method to transfer a consecutive block of bytes
(stored in a Byte Array) into a String? If I could find that I believe
I''d be able to deliver satisfactory performance, as this is currently my
bottleneck. I''ve looked at System.Text.Encoding.Unicode.GetString but
can''t seem to make it work properly!

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don''t.


Glad I could help.

What would be interesting from your point of view is to find what ''bits'' are
taking the time.

For example:

How long does it take just to read the input file?

Dim _start As DateTime = DateTime.Now
Dim _sr As New StreamReader(sInputFile)
Dim _chars(iRecordSize - 1) as Char
While sr.Peek() >= 0
_sr.Read(_chars, 0, _chars.Length)
Loop
_sr.Close()
Console.WriteLine(DateTime.Now.Subtract(_start).To talMilliseconds)

Then add in variius ''bits'' and take note of the elepsed time.

You will soon find where the bottlenecks are and can concentrate on
techniques to reduce those.
"ShaneO" <sp****@optusnet.com.auwrote in message
news:45**********************@news.optusnet.com.au ...

Stephany Young wrote:

>I would be inclined to use a StreamReader something like this:

Dim _sr As New StreamReader(sInputFile)

Dim _chars(iRecordSize - 1) as Char

Dim _text As New StringBuilder

While sr.Peek() >= 0
_sr.Read(_chars, 0, _chars.Length)
_text.AppendFormat("{0},", (New String(_chars, 0, 8)).Trim)
_text.AppendFormat("{0},", (New String(_chars, 9, 60)).Trim)
...
'' For the last ''field'', do not append a comma
_text.AppendFormat("{0}", (New String(_chars, x, y)).Trim)
_text.Append(Environment.NewLine)
Loop

_sr.Close()

sOutPutText - _text.ToString

Thank-you Stephany for your very thorough answer.

I did try similar but still found the constant looping (around 100K
records) was still clobbering performance. I will, however, follow your
example more precisely and test if it works quicker.

In the meantime, I''ve been experimenting again with using ReadAllBytes and
have found some tweaks to gain some speed improvements. One in particular
and as you mentioned, String objects are not too efficient on repeated
operations, so simply removing the following line -

sOutPutText &= sA & "," & sB & vbCrLf

and modifying it to (an already Open File) -

Print(iFileOut, sA & "," & sB & vbCrLf)

has had a staggering 50% reduction in the overall execution time! (Now
down to >5 secs with other tweaks).

Do you know of a quick method to transfer a consecutive block of bytes
(stored in a Byte Array) into a String? If I could find that I believe
I''d be able to deliver satisfactory performance, as this is currently my
bottleneck. I''ve looked at System.Text.Encoding.Unicode.GetString but
can''t seem to make it work properly!

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don''t.



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

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