CheckSum

CheckSum 是一個很簡單的檢查方法,把內容拆解成一堆 byte (或word,dword), 如果把這些 bytes 相加起來得到的值一致,表示內容”應該”是沒問題.

using System;
using System.IO;
using System.Collections;

public class CheckSum
{
    private long sum=0;
    
    public CheckSum()
    {
    }

    public void Add( int i )
    {
        sum += i;
    }
    
    public void Add( byte b )
    {
        sum += b;
    }
    
    public long GetCheckSum()
    {
        return sum;
    }
}

public class MyClass
{
    public static void Main()
    {
        string filename;
        FileStream fs=null;
        CheckSum sum = new CheckSum();
        
        //Open the stream and read it back.
        Console.Write( "Please input filename:" );
        filename = Console.ReadLine();
        Console.WriteLine( "Trying to read {0}", filename );
        
        if( File.Exists( filename ) )
            Console.WriteLine( "{0} is existed.", filename );

        try
        {
            fs = File.Open( filename, FileMode.Open, FileAccess.Read );
            
            if( fs.CanRead )
            {
                int value=0;
                
                while( (value = fs.ReadByte()) >= 0)
                {
                    sum.Add( value );
                }
                
                Console.WriteLine( "Checksum = {0,8:x}", sum.GetCheckSum() );
            }
            else
                Console.WriteLine( "Cannot be read" );
        }
        catch( Exception ex )
        {
            Console.WriteLine( ex.Message );
        }
        finally
        {
            fs.Close();
        }
        
        RL();
    }
    
    #region Helper methods
    private static void RL()
    {
        Console.ReadLine();    
    }
    #endregion
}

城裡的月光

前一陣子把許美靜的”靜聽精彩13首”又拿出來聽,霎時間突然又回到快退伍的那段時光.
記得那時候許美靜也是個唱遍港台東南亞的歌手呢~
可是後來又出了一兩張唱片之後,旋即銷聲匿跡,不知道到哪兒去了…
特別喜歡他唱的”城裡的月光”與”鐵窗”,可能是跟當時在當兵有關係吧…
當兵的時候,常熬夜打資料,深夜回去睡的時候,看到撒滿地的月光跟掛在天上的月亮.
再想到,沒辦法回家,被關在兵營裡,眼淚就會不太爭氣地流下來…
城裡的月光
詞:陳佳明
曲:陳佳明
專輯:靜聽精彩13首
主唱:許美靜
每顆心上某一個地方 總有個記憶揮不散
每個深夜某一個地方 總有個最深的思量
世間萬千的變幻 愛把有情的人分兩端
心若知到靈犀的方向 那怕不能夠朝夕相伴
城裡的月光把夢照亮 請溫暖他心房
看透了人間聚散 能不能多點快樂片段
城裡的月光把夢照亮 請守候他身旁
若有一天能重逢 讓幸福撒滿整個夜晚