使用MemoryStream读取文件到二进制数组 [英] Reading A File Using MemoryStream To Binary Array

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

问题描述

大家好..我希望我的问题标题清楚所有..

@First ..我已经完成了创建应用程序上传文件然后将其转换为字符串数组二进制0,1 ..下面的代码

Hello everyone.. i hope the title of my question is clear to all..
@First.. i have done to create an application to upload a files then convert it to string array of Binary 0,1.. the code below

string filename = openFileDialog1.FileName;
FileInfo FileInf = new FileInfo(filename);
int len = openFileDialog1.FileName.Length;

byte[] DA = File.ReadAllBytes(filename);
string s = string.Join("", DA.Select(x => Convert.ToString(x, 2).PadLeft(8, '0')));
string[] a = s.Select(c => c.ToString()).ToArray();
for (int i = 0; i <= a.Length-1 ; i++)
{
   if (a[i] == "0")
  {
    Doing something....
  }
   else if (a[i] == "1")
  {
    Doing something....
  }
}





现在我对上面的代码有问题..它是非常慢的它不能拿任何大于1Mb的文件...或者不到..我知道这是因为文件转换为X8字节!但这就是我需要的......



所以..我在互联网上搜索的是(从MemoryStream中读取)这会更快..

和第二个更好的代码是



Now i have a problem with the code above.. is it's very slow and it can't take any file large than 1Mb maybe.. or less than.. i know it's because the file converted to X8 Bytes!! but that's what i need..

So.. What i search on internet, is to (Read from MemoryStream) this's gonna be faster..
and the 2nd better code is

string path = openFileDialog1.FileName;
byte[] file = File.ReadAllBytes(path);
MemoryStream memory = new MemoryStream(file);
BinaryReader reader = new BinaryReader(memory);

 for (int i = 0; i < file.Length; i++)
 {
  byte result = reader.ReadByte();
  MessageBox.Show(result.ToString());
 }





现在,第二代码我觉得它很好,因为我试图加载大约1GB的文件,并且没有滞后或停止工作的消息!! .. 这是非常好的。但是这段代码给了我一个十进制值而且我不需要那个..

所有我需要的是让我的应用程序加载一个没有任何最慢读数的大文件并且还将字节转换为二进制索引的(8个索引中的8位)

对于ex ..

第一个字节文件是117 ..

i需要直接在8个索引中使用

0

1

1

1

0

1

0

1

完成后...再读一个字节..到最后。



请接受我最诚挚的问候,&我已准备好解释更多细节..



Now, The Second Code i feel it's good because i tried to load about 1GB file, and there's no lag or stopped working message!!.. that's very good. but this code gave me a decimal value and i don't need that..
All of what i need is to make my application loaded an large file without any slowest reading And Also convert byte after byte to binary index's (8 bits in 8 index's)
For ex..
First byte in the file is 117..
i need to make it directly in the 8 index's
0
1
1
1
0
1
0
1
After done.. read Another 1 byte.. TO THE END.

Please accept my best regards, & i'm ready to explain more details..

推荐答案

以防这些实用方法可能对您有所帮助:
Just in case these utility methods may assist you:
public static List<bool> BinaryStringToBitsAreOnes(string theString)
{
    return theString.Select(ch => ch == '1').ToList();
}

public static List<string> StringToCharsAsBinary(string theString)
{
    return theString.Select(ch => Convert.ToString(ch, 2).PadLeft(8, '0')).ToList();
}

示例测试:在某些方法或EventHandler中:

Sample test: in some method or EventHandler:

List<string> results = StringToCharsAsBinary("abc");

List<bool> bools = BinaryStringToBitsAreOnes(results[0]);

//在VStudio输出窗口中查看的示例输出:

// sample output as viewed in VStudio Output Window:

> ? results
Count = 3
    [0]: "01100001"
    [1]: "01100010"
    [2]: "01100011"
> ? bools
Count = 8
    [0]: false
    [1]: true
    [2]: true
    [3]: false
    [4]: false
    [5]: false
    [6]: false
    [7]: true
>

编辑:我认为系统。 Collections.BitArray可以帮助你;示例:

I think the System.Collections.BitArray may assist you; example:

public static BitArray ByteToBits(byte theByte)
{
    return new BitArray(new byte[]{theByte});
}

// sample use in some method or EventHandler

byte aByte = 63;

BitArray bArray = ByteToBits(aByte);

foreach (var val in bArray)
{
    Console.WriteLine(val);
}

这会产生如下输出:

True
True
True
True
True
True
False
False

请注意,如果您创建一个具有多个字节的BitArray,您将获得一个包含每个Byte八个布尔标志的Array。



有关其各种构造函数的信息,请参阅BitArray上的MS文档,并使用:[ ^ ]。



编辑:如何使用包含评估多个字节的结果的BitArray。

Note that if you create a BitArray with multiple Bytes, you will get an Array containing eight boolean flags per Byte.

See the MS documentation on BitArray for information about its various constructors, and uses: [^].

how to use a BitArray that holds the results of evaluating many Bytes.

public static List<bool> BytesToListOfBools(byte[] theBytes)
{
    // BitArray implements IEnumerable !
    return new BitArray(theBytes).Cast<bool>().ToList();
}

// sample use: execute in a method or EventHandler

byte[] bytes = new byte[] {0, 2, 4, 8, 16, 32, 64, 128};

List<bool> listOfByteAray = BytesToListOfBools(bytes);

StringBuilder sb = new StringBuilder();

for (int i = 0; i < listOfByteAray.Count; i += 8)
{
    var >Output of the above:<pre lang="text">False, False, False, False, False, False, False, False
False, True, False, False, False, False, False, False
False, False, True, False, False, False, False, False
False, False, False, True, False, False, False, False
False, False, False, False, True, False, False, False
False, False, False, False, False, True, False, False
False, False, False, False, False, False, True, False
False, False, False, False, False, False, False, True


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

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