Friday, February 5, 2010

C# Convert time to seconds since Jan 1, 2000

0 comments
Scenario
We know that the build-in DateTime object provide us a very convenience way to manipulate the number of seconds of time.  However, for hardware device, there might be a limit with the buffer during the transmission.  The DateTime starts counting the time since Jan 1, 0000.  We will have 63082281600000000000 nanoseconds until Jan 1, 2000.  For most of cases, we are not interested in any data between year 0000 and year 2000.  Therefore, the following methods just give an alternative way to calculate and convert the number of seconds from DateTime object.

Method 
        // Since 12:00:00 am Jan 1, 2000
        public const long INITIAL_DATETIME = 630822816000000000;

        // Convert current time to number of seconds since Jan 1, 2000.
        public static int GetTimeForDevice()
        {
            int ans = 0;

            ans = (int)((DateTime.Now.Ticks - INITIAL_DATETIME) / 10000000);

            return ans;
        }

        // Convert time from number of seconds since Jan 1, 2000 to a readable string
        public static string GetTimeFromDevice(string time)
        {
            string ans = "";

            long temp = Int32.Parse(time) * 10000000 + INITIAL_DATETIME;
            DateTime dt = new DateTime(temp);
            ans = dt.ToShortDateString() + " " + dt.ToLongTimeString();

            return ans;
        }

Friday, June 19, 2009

C# How to convert byte arry to string

0 comments
Scenario
How to a byte array to a string? The solution is using the 'GetString()' method from the ASCIIEncoding.ASCII object. Be carrefull with this method, it may cause problems if you try to convert something other than English, such as Chinese and other asian languages. In this case, you have to use specific ASCII encoding for that language.
For example, you probably want to use System.Text.Encoding.GetEncoding(1251).GetString(b);
Here, '1251' stands for English. You can choose your own codepage for Encoding or Decoding.

Method
byte[] data = ... ; //whatever it is here
string text1 = System.Text.ASCIIEncoding.ASCII.GetString(data);//Let the ASCIIEncoding handle the convertion
string text2 = System.Text.Encoding.GetEncoding(1251).GetString(data);// 1251 stands for English
string text3 = System.Text.Encoding.GetEncoding(28591).GetString(data);// 28591 stands for Latin
string text4 = System.Text.Encoding.GetEncoding("GB18030").GetString(data);// "GB18030" stands for Simplified Chinese

Thursday, June 11, 2009

C# How to beep from your computer

0 comments
Scenario
How to make a beep sound from your computer?
Although you can use any sound library to work for you, there is a simple way to make just a beep sound. This becomes useful especially when you are running a program for a long time and you want to be notified when any certain condition reaches.
Thanks to Microsoft, we can achieve this in one line of code with the build-in methods.

Method
Console.Beep();//No parameter
Console.Beep(250, 250);//Parameter: int frequency, int duration

Wednesday, May 13, 2009

C# Convert string to an array of bytes

0 comments
Scenario
How to convert a string or a string array into a double array?
The .NET does not provide any method for us to call directly(if you found it, please let me know :D). However this is easy to implement within just a couple lines of code.

Method
//Convert 1 string to an array of bytes
public static byte[] ConvertToBytes(string str)
{
byte[] ans = new byte[str.Length];

for (int i = 0; i < ans.Length; i++)
{
ans[i] = Convert.ToByte(str[i]);
}

return ans;
}

Tuesday, May 5, 2009

How to disable a TabPage in the TabControl

0 comments

Scenario
The component TabControl in the Visual Studio IDE does not allow people to
disable a certain TabPage within the TabControl. I have Googled for a long time
and there is not any real solution regarding this issue. However, some people put a
customized TabControl which allows you to disable the TabPage. I believe not
everyone wants to download and try the third party software or plug-in.

Method
I'm going to talk about a 'solution' which is more like a trick to solve this problem.
For example, let's create a TabControl and it has 2 TabPages. Now before
dragging any other objects from the tool list, drag a Panel object and drop into
each one of those TabPages. Set those three Panels' Dock property to Fill so
that the panel will be maximized within its TabPage. Now you will probably guess
what I'm trying to tell you. Yes, you put all other objects on top of the panel.
Then you just disable the panel if you want to disable a TabPage. Because the
Panel is the Parent of the other components, the panel and its children will be
disabled if we try to disable the panel itself.

Details
Let's have a look at how this works.

First, create a TabControl and drop a Panel onto one of the tab pages.


Now, set the Dock to Fill for the panel


Add some components to the tabPage1. Actually they are on top of the
panel1. Run the sample application without checking the 'Enable Panel1'
checkbox.


The code implementation for the checkbox. Just for testing.


The effect shows the tabPage1 is 'disabled' once the checkbox is unchecked.

Summary
This solution might not be perfect to everyone, but I believe it will help some
people like me to solve a problem by using a simple solution. Remember the KISS
principle? This is all about it!

Sunday, May 3, 2009

Windows 7's Hardware Requirement

0 comments

The hardware requirements on different Windows Operating Systems

The Windows 7 RC version has been released on Apr 30, 2009 for Technet subscribers only. On May 5 everyone should be able to get it for free. Here is the hardware requirement for installing Windows 7 RC on your computer. We can see that it is about the same as Windows Vista, but Windows 7 is 3 years later than Windows Vista.

  Windows 7 Hardware Requirements:

  ·CPU 1GHz(32 bits OR 64 bits)。

  ·1GB Memory(32 Bits);2GB(64 Bits)。

  ·16GB Hard Drive(32 Bits);20GB Hard Drive(64 Bits)。

  ·Support WDDM 1.0 or higher DirectX 9

Friday, May 1, 2009

Convert File Size To Readable Format

0 comments
Scenario
When we use "Windows + E" to browse files, we will see the file size of each file under the "Details" mode. Every file has a file size, but by default all file sizes are displayed in one unit 'KB'. This makes people difficult to make sense if the file size is greater than 10MB, because you will see a lot of numeric digits before the dot symbol.

Method
The following table is not magic and the issue mentioned above is not hard to solve. Here is a way to convert the file size into human readable format under normal circumstance.

1 KB = 1024 Byte = 1024 Byte
1 MB = 1024 KB = 1024 * 1024 Byte = 1048576 Byte
1 GB = 1024 MB = 1024 * 1024 KB = 1024 * 1024 * 1024 Byte = 1073741824 Byte


To convert the file size you retrieve from the original "File.Length" method, we can use the above table to check the byte count.

Code

Conclusion
As you see, after calling the "GetFileSize" method, it will return a converted format of file size string. The implementation is only a few if statements.
You can copy & modify the above code for your convenience.

BlogUpp!

 

Copyright 2009 All Rights Reserved Revolution Two Lifestyle theme by Brian Gardner | Blogger template converted & enhanced by eBlog Templates