This blog has, IMO, some great resources. Unfortunately, some of those resources are becoming less relevant. I'm still blogging, learning tech and helping others...please find me at my new home on http://www.jameschambers.com/.

Thursday, April 30, 2009

Immediately Useful - BB>187

There is a part of a web admin console that we have that displays a MAC address of a device, which our network admin routinely punches in value by value into Calculator and converts it to a decimal format:

  AC:0F:BB:EF:01:CE

…and that becomes:

172.15.187.239.1.206

He has to do that because there is another utility that expects the decimal format of the MAC address.

AND, this has to be done each time we add a new device/customer/end point on our network.

I am using c# to convert the MAC address to decimal in this example.  .Net provides some quick-and-easy ways to convert from hex to decimal and this is ideal when converting a MAC address.

There are a number of approaches on how to solve this kind of issue, but I chose one that doesn’t require (much of) a user interface.  The only visible aspect of the program is a task tray icon that, when double-clicked, converts the contents of the clipboard from hex to decimal.

I chose this approach because, for the most part, we’re dealing with one (or few) of these conversions at a time.  He can copy, double-click and paste into the other app without having to introduce a third, windowed interface into the mix.

Here’s the meat of the method that does the work:

 

try
{
// grab whatever's on the clipboard
// and try to break it apart
string input = Clipboard.GetText();
string[] parts = input.Split(':');

// convert the data to integers
// and build a string
StringBuilder sb = new StringBuilder();
foreach (string part in parts)
{
sb.Append(int.Parse(part, NumberStyles.AllowHexSpecifier));
sb.Append(".");
}

// need to drop that last . from the
// string and set the clipboard
string result = sb.ToString();
Clipboard.SetText(result.Substring(0, result.Length - 1));
}
catch (Exception)
{
// nothing fancy here, anything goes
// wrong and we bail...
Clipboard.SetText("Hrm...Bad data...");
}



Put that into a double-click event handler for a NotifyIcon and you’re sailing.

No comments:

Post a Comment