|
查看: 1407|回复: 7
|
C# 中的 File Reading, 要整样 read file BIT BY BIT
[复制链接]
|
|
|
在 C# 里,
要整样把 file read BIT BY BIT ???
样本:
101010000111111000000 .........
我只能 read BYTE, 但是我要 BIT BY BIT....
谢谢帮忙.  |
|
|
|
|
|
|
|
|
|
|
发表于 20-10-2008 10:28 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 19-11-2008 12:02 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 19-11-2008 07:17 PM
|
显示全部楼层
抛砖引玉
LINQ 第一次
using (FileStream fs = new FileStream(@"D:\test.txt", FileMode.Open, FileAccess.Read))
{
long numBytes = fs.Length;
BinaryReader br = new BinaryReader(fs);
var bits = br.ReadBytes((int)numBytes)
.SelectBit();
string xxx = "";
foreach (var bit in bits)
{
xxx += bit.ToString();
}
}
public static IEnumerable<int> SelectBit(this IEnumerable<byte> source)
{
foreach (var s in source)
{
byte x = s;
for (int i = 0; i < 8; i++)
{
yield return (x & 0x80) != 0 ? 1 : 0;
x <<= 1;
}
}
} |
|
|
|
|
|
|
|
|
|
|
发表于 21-11-2008 06:54 PM
|
显示全部楼层
新方法
static IEnumerable<int> ReadFile(string path)
{
using (Stream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
int iRead;
do
{
iRead = fs.ReadByte();
yield return iRead;
}
while (iRead != -1);
}
}
public static IEnumerable<int> SelectBit(this IEnumerable<byte> source)
{
foreach (var s in source)
{
byte x = s;
for (int i = 0; i < 8; i++)
{
yield return (x & 0x80) != 0 ? 1 : 0;
x <<= 1;
}
}
} |
|
|
|
|
|
|
|
|
|
|
发表于 21-11-2008 06:56 PM
|
显示全部楼层
普通call法
var bits = ReadFile(@"D:\test.txt")
.Select(x => (byte)x)
.SelectBit();
foreach (var bit in bits)
{
Console.WriteLine("Result retrieved with bit value=" + bit);
} |
|
|
|
|
|
|
|
|
|
|
发表于 21-11-2008 06:57 PM
|
显示全部楼层
Asynchronous call 法
Func<string, IEnumerable<int>> asyncMethodCall = ReadFile;
asyncMethodCall.BeginInvoke(@"D:\test.txt", delegate(IAsyncResult result)
{
var value = asyncMethodCall.EndInvoke(result)
.Select(x => (byte)x)
.SelectBit();
foreach (var i in value)
Console.WriteLine("Result retrieved with bit value=" + i);
}, null); |
|
|
|
|
|
|
|
|
|
|
发表于 21-11-2008 06:57 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|