#990 – Converting Hexadecimal Strings to Numeric Data

You can convert a numeric value to its equivalent hex string using the hexadecimal format specifier.  To convert in the other direction, from a hex string to its corresponding integer value, you use the int.Parse function, passing NumberStyles.AllowHexSpecifier as the second parameter.

            // using System.Globalization
            int n = int.Parse("A", NumberStyles.AllowHexSpecifier);
            int n2 = int.Parse("9D", NumberStyles.AllowHexSpecifier);
            int n3 = int.Parse("400", NumberStyles.AllowHexSpecifier);
            int n4 = int.Parse("1b3f", NumberStyles.AllowHexSpecifier);
            int n5 = int.Parse("000b", NumberStyles.AllowHexSpecifier);

990-001

Advertisement

#989 – Formatting Numbers as Hexadecimal

You can represent an integer as a series of hexadecimal characters by using the hexadecimal format specifier in a format string.

In the examples below, we represent several integers using their hexadecimal characters.  We use the X notation in the format string to indicate that the number should be displayed as hex.

            Console.WriteLine("{0} = {0:X}", 10);    // A
            Console.WriteLine("{0} = {0:X}", 157);   // 9D = (9 * 16) + 13
            Console.WriteLine("{0} = {0:X}", 1024);  // 400 = (4 * 256)
            Console.WriteLine("{0} = {0:X}", 6975);  // 1B3F = (1 * 4096) + (11 * 256) + (3 * 16) + 15

989-001
You can include a digit after the X to indicate the number of minimum digits to be displayed. The hex number will be padded with zeroes to reach the desired width.

            Console.WriteLine("{0} = {0:X4}", 157);   // 009D

989-002

#988 – Using global to Explicitly Refer to a Namespace

A fully qualified namespace is not always sufficient to refer to a type.  Below, we’ve (unwisely) declared two different classes named Utility–a subclass of Program in the ConsoleApplication1 namespace and a class in the Program namespace.

If we refer to Program.Utility from within the Main method, the subclass is used.  To use the other version of Utility, we need to use the global keyword to describe the exact path to the type.  global::Program refers to the top-level namespace Program.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Class Program.Utility in namespace ConsoleApplication1
            Program.Utility util = new Program.Utility();

            // Class Utility in namespace Program
            global::Program.Utility util2 = new global::Program.Utility();
        }

        class Utility
        {
            public Utility()
            {
                Trace.WriteLine("Class Program.Utility in namespace ConsoleApplication1");
            }
        }
    }

}

namespace Program
{
    public class Utility
    {
        public Utility()
        {
            Trace.WriteLine("Class Utility in namespace Program");
        }
    }
}

#987 – The using Directive Can Create an Alias for a Type

You can use the using directive to create an alias for a namespace, which you can then use to access the types in that namespace.  For example:

using U1 = DogLibrary.Utility.StandardLogging;
using U2 = DogLibrary.Utility.AlternateLogging;

We might do the above if we had an identically named type in each of the two namespaces so that we could then prefix the type with the appropriate namespace.

U1.DogLogger log1 = new U1.DogLogger(@"C:\log1.txt");
U2.DogLogger log2 = new U2.DogLogger(@"C:\log2.txt");

We can also use the using directive to create an alias for a specific type.  For the example above, we could do the following:

using Logger1 = DogLibrary.Utility.StandardLogging.DogLogger;
using Logger2 = DogLibrary.Utility.AlternateLogging.DogLogger;

We could then use the alias directly as a type name:

            // Short for DogLibrary.Utility.StandardLogging.DogLogger
            Logger1 log1 = new Logger1(@"C:\log1.txt");

            // Short for DogLibrary.Utility.AlternateLogging.DogLogger
            Logger2 log2 = new Logger2(@"C:\log2.txt");

#986 – Using goto to Jump to a Label

You can use the goto statement within a block of code to explicitly jump to another location in the code using a label.

            Dog d = new Dog("Bob", 5);

        DoTraining:
            // Train my dog
            d.Train();
            if (d.NumMinutesCanSit < 5)
                goto DoTraining;

            Console.WriteLine("My dog is trained!");

While you can use a goto statement to jump to a label, it’s almost always a bad idea to use goto in this way.  You can always use structured programming techniques, like the while statement, rather than goto.  Code containing goto statements is typically harder to understand than functionally equivalent code written using structured programming constructs.

Here’s the above block of code, re-written to use a while loop.

            do
                d.Train();
            while (d.NumMinutesCanSit < 5);