Thursday, May 27, 2010

C# Notes

Looping threw a range
foreach(int i in Enumerable.Range(min_number, max_number))
{
    Console.WriteLine(i);
}

String to int
Convert.ToInt32(string)

Reading a text file
StreamReader sr = new StreamReader(file_path);
string data = sr.ReadLine();
sr.Close();

Checking for whole number
if((someValue % 1) == 0)
{
    Console.WriteLine("someValue is a whole number");
}

Create random int
Random random = new Random();
int randInt = random.Next(min, max);
Generate md5 hash from string
string GetMd5Hash(string inputString)
{ 
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

    byte[] byteString = Encoding.UTF8.GetBytes(inputString);
    byteString = md5.ComputeHash(byteString);

    StringBuilder stringBuilder = new StringBuilder();

    foreach(byte b in byteString)
    {
        stringBuilder.Append(b.ToString("x2").ToLower());
    }

    string hashString = stringBuilder.ToString();
    return hashString;
}

Wednesday, May 19, 2010

Formatting an external hard drive in windows

Recently I was playing around with a 160GB western digital hard drive. I came across some adapters to plug in the HD via USB. I needed to format the drive so I went to 'run' and typed in "diskmgmt.msc" with out quotes. This runs windows disk management utility. I found my drive, right clicked, and hit format. The program took about an hour to complete the format.