佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

搜索
查看: 1407|回复: 7

C# 中的 File Reading, 要整样 read file BIT BY BIT

[复制链接]
发表于 22-9-2008 05:41 PM | 显示全部楼层 |阅读模式
在 C#  里,
要整样把 file read BIT BY BIT ???

样本:
101010000111111000000 .........

我只能 read BYTE, 但是我要 BIT BY BIT....

谢谢帮忙.
回复

使用道具 举报


ADVERTISEMENT

发表于 20-10-2008 10:28 AM | 显示全部楼层
回复

使用道具 举报

发表于 19-11-2008 12:02 AM | 显示全部楼层
原帖由 heng81 于 22-9-2008 05:41 PM 发表
在 C#  里,
要整样把 file read BIT BY BIT ???

样本:
101010000111111000000 .........

我只能 read BYTE, 但是我要 BIT BY BIT....

谢谢帮忙.

如果你用file stream 的话,理论上来讲是不能的,因为那个position marker 每次走一下就是1byte.
可是你不能读了1byte, 然后用bit masking来回传一个个资料吗?
或者出绝招,用个counter,每次读的时候加一,如果小过8,把position marker 退回去
回复

使用道具 举报

发表于 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);
        }
回复

使用道具 举报

Follow Us
发表于 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 | 显示全部楼层
收工 .........
回复

使用道具 举报


ADVERTISEMENT

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT


本周最热论坛帖子本周最热论坛帖子

ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2026 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 2-5-2026 03:11 AM , Processed in 0.080870 second(s), 12 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表