如何使用Stream Reader读取特定的数据块? [英] How can I use Stream Reader to read a specific block of data?

查看:426
本文介绍了如何使用Stream Reader读取特定的数据块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码的一部分:

This is a portion of my code:

Using sr As New StreamReader(myFile)
Dim bt As Byte() = Encoding.ASCII.GetBytes(sr.ReadBlock(buffer1, index1, increment))



基本上,我想将特定的字节块转换为ascii.我不想从流的开头开始.我目前不确定,但是也许将第25位读到第50位并将其转换为ascii.

我不关心转换部分;只是特定的块.我想我上面的语法正确.我可以将这些变量替换为特定的值吗?我必须使用变量吗?我希望这一切都说得通!任何帮助将不胜感激.



Basically, I would like to convert a specific block of bytes to ascii. I do not want to start from the beginning of the stream. I''m not sure at the moment, but perhaps read the 25th bit to the 50th bit and convert it to ascii.

I''m not concerned about the conversion part; just the specific block. I think I have the syntax correct above. Can I replace those variables with specific values? Must I use the variables? I hope this all makes sense! Any help would be greatly appreciated.

推荐答案

必须使用变量吗?".哇.我还要打扰吗?

好的,您只需要跳过n的前几个字节.读取前使用StreamReader.BaseStream.Position转发流位置.

现在,如果您的编码是ASCII,则无需转换任何ASCII格式的内容.请执行以下操作:
New StreamReader(myFile, System.Text.ASCIIEncoding.ASCII).

"Must I use the variables?". Wow. Should I even bother to ask?

OK, all you need to be able to skip n first bytes. Use StreamReader.BaseStream.Position to forward stream position before reading.

Now, you don''t need to convert anything in ASCII if your encoding is ASCII. Do this:
New StreamReader(myFile, System.Text.ASCIIEncoding.ASCII).

Using sr As New StreamReader(myFileName, System.Text.ASCIIEncoding.ASCII)
    ' jump stream forward by 3 bytes (or as many as you need)
    ' as ASCII character is always 1 byte, you shift by first 3 characters:
    st.BaseStream.Position = 3
    ' keep reading as you did before
End Using 



希望对您有帮助.

—SA



Hope, it helps you.

—SA


这篇关于如何使用Stream Reader读取特定的数据块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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