Hex String to byte array converter

C#, C# Language 6 Comments »

The following simple static class will take a string of Hexdecimal characters and convert it to a byte array.

 

    static class HexStringConverter
    {
        public static byte[] ToByteArray(String HexString)
        {
            int NumberChars = HexString.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
            }
            return bytes;
        }
    }

Writing and Reading AVI files in C#

C#, External C# Articles, Graphics and Images 1 Comment »

Here is a very extensive article, with demos and code written by Corinna John on the CodeProject.

http://www.codeproject.com/cs/media/avifilewrapper.asp

Nullable Types

C#, C# Language No Comments »

In C# and .net you can define a variable of the standard values types to be nullable as well. This is extremely useful in circumstances when you want to check if a varaible has been intialized correctly.

For example, if you have a class that represents a user which has an integer user id and it is not initialized in the constructor. Rather than having the user id intitialize to 0 (zero) (which would be the default for an integer) as the User id 0 may well be a valid user’s id. the type can be defined as a nullable integer using int? UserID and will default to null instead.

Examples of nullable declarations

[csharp]

int? i = 10;
double? d1 = 3.14;
bool? flag = null;
char? letter = ‘a’;
int?[] arr = new int?[10];

[/csharp]

Custom Exceptions and Serialization.

C#, C# Language, Win Forms No Comments »

Microsoft state that if you want to raise custom exceptions in your application then you should derive them from the ApplicationException class and not the Exception class.

ApplicationException is thrown by a user program, not by the common language runtime. If you are designing an application that needs to create its own exceptions, derive from the ApplicationException class. ApplicationException extends Exception, but does not add new functionality. This exception is provided as means to differentiate between exceptions defined by applications versus exceptions defined by the system.

However, the blog entry by Microsoft’s Brad Adams states that ApplicationException should not be used !

I found that descending from Exception, at least gives a nice error message detailing the exception that was raised as opposed to ApplicationExceptiosn, “Application Error ocurred” message.

So how do we serialize a custom exception class ?

Step 1) Add the Serializable attribute to the exception

Step 2) Provide a contructor that takes the serialization information and context e.g. MyException(SerializationInfo info, StreamingContext context)
Step 3) If you have a constructor that passes state information to the exception to be included in the data collection ensure this is copied from the exception object to the info in the serialization constructor (step2)

e.g.

[csharp]

[Serializable]
public class MyException : Exception
{
public MyException(String sessionId)
: base()
{
this.Data.Add(”SessionID”, sessionId);
}

public MyException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
info.AddValue(”SessionID”,Data["SessionID"]);
}

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
}
[/csharp]

WP Theme &Design by minus19.com & Icons
Entries RSS Comments RSS Log in