| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1054 人关注过本帖
标题:请问各位大大们下面的一组C#代码的意思是什么 不太明白
只看楼主 加入收藏
ragnarok
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-1-24
结帖率:33.33%
收藏
已结贴  问题点数:18 回复次数:1 
请问各位大大们下面的一组C#代码的意思是什么 不太明白
using System;

namespace DQ7
{
    class SaveData
    {
        private static SaveData mThis;
        private String mFileName = null;
        private Byte[] mBuffer = null;
        private readonly System.Text.Encoding mEncode = System.Text.Encoding.UTF8;

        private SaveData()
        {}

        public static SaveData Instance()
        {
            if (mThis == null) mThis = new SaveData();
            return mThis;
        }

        public bool Open(String filename, bool force)
        {
            mFileName = filename;
            mBuffer = (mFileName);

            if (force || CheckSum() == ReadNumber(0, 4))
            {
                Backup();
                return true;
            }

            mFileName = null;
            mBuffer = null;
            return false;
        }

        public bool Save()
        {
            if (mFileName == null || mBuffer == null) return false;
            WriteNumber(0, 4, CheckSum());
            (mFileName, mBuffer);
            return true;
        }

        public bool SaveAs(String filenname)
        {
            if (mBuffer == null) return false;
            mFileName = filenname;
            return Save();
        }

        public uint ReadNumber(uint address, uint size)
        {
            if (mBuffer == null) return 0;
            address = CalcAddress(address);
            if (address + size > mBuffer.Length) return 0;
            uint result = 0;
            for(int i = 0; i < size; i++)
            {
                result += (uint)(mBuffer[address + i]) << (i * 8);
            }
            return result;
        }

        // 0 to 7.
        public bool ReadBit(uint address, uint bit)
        {
            if (bit < 0) return false;
            if (bit > 7) return false;
            if (mBuffer == null) return false;
            address = CalcAddress(address);
            if (address > mBuffer.Length) return false;
            Byte mask = (Byte)(1 << (int)bit);
            Byte result = (Byte)(mBuffer[address] & mask);
            return result != 0;
        }

        public String ReadText(uint address, uint size)
        {
            if (mBuffer == null) return "";
            address = CalcAddress(address);
            if (address + size > mBuffer.Length) return "";

            Byte[] tmp = new Byte[size];
            for(uint i = 0; i < size; i++)
            {
                if (mBuffer[address + i] == 0) break;
                tmp[i] = mBuffer[address + i];
            }
            return mEncode.GetString(tmp).Trim('\0');
        }

        public void WriteNumber(uint address, uint size, uint value)
        {
            if (mBuffer == null) return;
            address = CalcAddress(address);
            if (address + size > mBuffer.Length) return;
            for (uint i = 0; i < size; i++)
            {
                mBuffer[address + i] = (Byte)(value & 0xFF);
                value >>= 8;
            }
        }

        // 0 to 7.
        public void WriteBit(uint address, uint bit, bool value)
        {
            if (bit < 0) return;
            if (bit > 7) return;
            if (mBuffer == null) return;
            address = CalcAddress(address);
            if (address > mBuffer.Length) return;
            Byte mask = (Byte)(1 << (int)bit);
            if (value) mBuffer[address] = (Byte)(mBuffer[address] | mask);
            else mBuffer[address] = (Byte)(mBuffer[address] & ~mask);
        }

        public void WriteText(uint address, uint size, String value)
        {
            if (mBuffer == null) return;
            address = CalcAddress(address);
            if (address + size > mBuffer.Length) return;
            Byte[] tmp = mEncode.GetBytes(value);
            Array.Resize(ref tmp, (int)size);
            for (uint i = 0; i < size; i++)
            {
                mBuffer[address + i] = tmp[i];
            }
        }

        public void Fill(uint address, uint size, Byte number)
        {
            if (mBuffer == null) return;
            address = CalcAddress(address);
            if (address + size > mBuffer.Length) return;
            for (uint i = 0; i < size; i++)
            {
                mBuffer[address + i] = number;
            }
        }

        public void Copy(uint from, uint to, uint size)
        {
            if (mBuffer == null) return;
            from = CalcAddress(from);
            to = CalcAddress(to);
            if (from + size > mBuffer.Length) return;
            if (to + size > mBuffer.Length) return;
            for(uint i = 0; i < size; i++)
            {
                mBuffer[to + i] = mBuffer[from + i];
            }
        }

        public void Swap(uint from, uint to, uint size)
        {
            if (mBuffer == null) return;
            from = CalcAddress(from);
            to = CalcAddress(to);
            if (from + size > mBuffer.Length) return;
            if (to + size > mBuffer.Length) return;
            for (uint i = 0; i < size; i++)
            {
                Byte tmp = mBuffer[to + i];
                mBuffer[to + i] = mBuffer[from + i];
                mBuffer[from + i] = tmp;
            }
        }

        private uint CheckSum()
        {
            int result = 0;
            for (uint i = 0x10; i < mBuffer.Length; i++)
            {
                result += (SByte)ReadNumber(i, 1);
            }
            return (uint)result;
        }

        private uint CalcAddress(uint address)
        {
            return address;
        }

        private void Backup()
        {
            DateTime now = DateTime.Now;
            String path = (System.Reflection.Assembly.GetExecutingAssembly().Location);
            path = (path, "backup");
            if(!(path))
            {
                (path);
            }
            path = (path,
                String.Format("{0:0000}-{1:00}-{2:00} {3:00}-{4:00}", now.Year, now.Month, now.Day, now.Hour, now.Minute));
            (path, mBuffer);
        }
    }
}
搜索更多相关主题的帖子: null return size Byte address 
2023-01-27 21:22
dengligsh
Rank: 2
等 级:论坛游民
威 望:3
帖 子:4
专家分:18
注 册:2023-2-3
收藏
得分:18 
这段代码是C#语言中的一段类的代码,命名为SaveData,主要功能是读写二进制文件,通过读取指定的地址和大小进行读写数字、文本、二进制位等。

这个类通过设计单例模式,在整个应用程序内只有一个实例,使用静态方法Instance()来进行访问,可以打开或保存二进制文件,读取或写入数字、文本、二进制位。

类中还使用了一个私有字段mBuffer,用于存储二进制文件的内容,并通过读写方法实现对二进制文件内容的读写操作。
收到的鲜花
  • 龙胆草2023-02-07 16:01 送鲜花  1朵  
2023-02-03 15:25
快速回复:请问各位大大们下面的一组C#代码的意思是什么 不太明白
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025748 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved