I wrote the following code:
Byte test = new Byte();
test = 190;
Debug.Print(test.ToString("X2"));
And I get this:
#### Exception System.ArgumentException - 0x00000000 (3) ####
Anyone know why??
Convert a Byte to Hex
Started by Charles, Nov 26 2010 04:54 PM
5 replies to this topic
#1
Posted 26 November 2010 - 04:54 PM
#2
Posted 26 November 2010 - 04:58 PM
.NET Micro Framework doesn't support hex conversion/formatting using .ToString("X2"). This is one of the areas where Microsoft needed to reduce the footprint of the runtime.
There are a number of hex conversion routines for .NET MF on the web. I know that we've built one too. If you have trouble finding one, please post here and I'll see if I can track one down for you...
Chris
#3
Posted 26 November 2010 - 05:46 PM
Thanks for clearing that up! Thought I was losing my mind...
#4
Posted 26 November 2010 - 08:52 PM
For anyone who needs this, here's a handy code snipplet to make the N+ cough up its MAC address.
It is loosely based on some examples off the Internet for converting hex codes, but has been modified significantly to improve readability/strip out unnecessary code.
It is loosely based on some examples off the Internet for converting hex codes, but has been modified significantly to improve readability/strip out unnecessary code.
// Access variable to the network subsystem. NetworkInterface[] netIF = NetworkInterface.GetAllNetworkInterfaces(); string macAddress = ""; // Create a character array for hexidecimal conversion. const string hexChars = "0123456789ABCDEF"; // Loop through the bytes. for (int b = 0; b < 6; b++) { // Grab the top 4 bits and append the hex equivalent to the return string. macAddress += hexChars[netIF[0].PhysicalAddress[b] >> 4]; // Mask off the upper 4 bits to get the rest of it. macAddress += hexChars[netIF[0].PhysicalAddress[b] & 0x0F]; // Add the dash only if the MAC address is not finished. if (b < 5) macAddress += "-"; } return macAddress;
#5
Posted 22 May 2011 - 09:34 AM
Thanks Charles, great code!
I know it's petty changes, but I changed the NetworkInterface variable to only store the single item, and I changed the int to a byte - saving a smidgen of RAM!
I know it's petty changes, but I changed the NetworkInterface variable to only store the single item, and I changed the int to a byte - saving a smidgen of RAM!
// Access variable to the network subsystem. NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[0]; string macAddress = ""; // Create a character array for hexidecimal conversion. const string hexChars = "0123456789ABCDEF"; // Loop through the bytes. for (byte b = 0; b < 6; b++) { // Grab the top 4 bits and append the hex equivalent to the return string. macAddress += hexChars[ni.PhysicalAddress[b] >> 4]; // Mask off the upper 4 bits to get the rest of it. macAddress += hexChars[ni.PhysicalAddress[b] & 0x0F]; // Add the dash only if the MAC address is not finished. if (b < 5) macAddress += "-"; } return macAddress;
#6
Posted 22 May 2011 - 06:59 PM
If it is helpful I have been using this:
Then you can call it like so:
public static class BitConverter { public static string ToString(byte[] value, int index = 0) { return ToString(value, index, value.Length - index); } public static string ToString(byte[] value, int index, int length) { char[] c = new char[length * 3]; byte b; for (int y = 0, x = 0; y < length; ++y, ++x) { b = (byte)(value[index + y] >> 4); c[x] = (char)(b > 9 ? b + 0x37 : b + 0x30); b = (byte)(value[index + y] & 0xF); c[++x] = (char)(b > 9 ? b + 0x37 : b + 0x30); c[++x] = '-'; } return new string(c, 0, c.Length - 1); } }
Then you can call it like so:
Debug.Print(BitConverter.ToString(net.PhysicalAddress));
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users