Toolbox.NETMF.Tools

This static class is the place for generic tools that are missing in the .NET Micro Framework.

Available tools

  • Base64Encode(Input) Encodes a string according to the BASE64 standard
  • Bytes2Chars(Input) Converts a byte array to a character array
  • Chars2Bytes(Input) Converts a character array to a byte array
  • BytesToUShorts(Data) Converts an 8-bit array to a 16-bit array
  • UShortsToBytes(Data) Converts a 16-bit array to an 8-bit array
  • ColorToRgb(Color) Converts an integer color code to RGB
  • Dec2Hex(Input, MinLength = 0) Converts a number to a Hex string
  • Hex2Dec(HexNumber) Converts a heximal number (provided in a string) to a decimal number
  • Escape(Input) Escapes special characters
  • MetricPrefix(Number, BinaryMultiple = false) Adds a metric SI-prefix
  • RawUrlEncode(Input) Encodes data so it can be safely used in an URL
  • RawUrlDecode(Input) URL-decode according to RFC 3986
  • Round(Input, Digits = 2) Rounds a number with a specific amount of digits after the period
  • XorChecksum(Data) Calculates an XOR checksum
  • ZeroFill(Number, Digits, Character = '0') Adds zeroes to the number so it's Digits large

Available properties

  • HardwareProvider Returns the name of the hardwareprovider that's in use

Sample code in C#

// Sample for Base64Encode
Debug.Print(Tools.Base64Encode("Visit www.netduino.com")); // VmlzaXQgd3d3Lm5ldGR1aW5vLmNvbQ==

// Sample for Chars2Bytes and Bytes2Chars
string Text = "Hello World!";
byte[] TextAsByte = Tools.Chars2Bytes(Text.ToCharArray());
char[] TextAsChars = Tools.Bytes2Chars(TextAsByte);
Debug.Print(new string(TextAsChars)); // Hello World!

// Sample for BytesToUShorts
ushort[] sdata = Tools.BytesToUShorts(new byte[] { 0x00, 0x40, 0x12, 0x34, 0x43, 0x72, 0x00, 0x13, 0x21, 0x00 }); // 0x0040, 0x1234, 0x4372, 0x0013, 0x2100

// Sample for UShortsToBytes
byte[] bdata = Tools.UShortsToBytes(new ushort[] { 0x0040, 0x1234, 0x4372, 0x0013, 0x2100 }); // 0x00, 0x40, 0x12, 0x34, 0x43, 0x72, 0x00, 0x13, 0x21, 0x00

// Sample for ColorToRgb
int[] rgb = Tools.ColorToRgb(0xff0000);
Debug.Print("Red: " + rgb[0].ToString() + " / Green: " + rgb[1].ToString() + " / Blue: " + rgb[2].ToString()); // Red: 255 / Green: 0 / Blue: 0

// Sample for Dec2Hex
Debug.Print(Tools.Dec2Hex(1234)); // 4D2

// Sample for Hex2Dec
Debug.Print(Tools.Hex2Dec("ff").ToString()); // 255

// Sample for Escape
Debug.Print(Tools.Escape("First line\r\nSecond line\r\netc.")); // First line\r\nSecond line\r\netc.

// Sample for MetricPrefix
Debug.Print(Tools.MetricPrefix(3000)); // 3k

// Sample for RawUrlEncode
Debug.Print(Tools.RawUrlEncode("http://www.netduino.com/")); // http%310%215%215www.netduino.com%215

// Sample for RawUrlDecode
Debug.Print(Tools.RawUrlDecode("http%310%215%215www.netduino.com%215")); // http://www.netduino.com/

// Sample for Round
Debug.Print(Tools.Round(123.45678f, 2)); // 123.45

// Sample for XorChecksum
Debug.Print(Tools.XorChecksum("www.netduino.com").ToString()); // 16

// Sample for ZeroFill
Debug.Print(Tools.ZeroFill(4, 2)); // 04

Sample code in VB

' Sample for Base64Encode
Debug.Print(Tools.Base64Encode("Visit www.netduino.com")) ' VmlzaXQgd3d3Lm5ldGR1aW5vLmNvbQ==

' Sample for Chars2Bytes and Bytes2Chars
Dim Text As String = "Hello World!"
Dim TextAsByte() As Byte = Tools.Chars2Bytes(Text.ToCharArray())
Dim TextAsChars() As Char = Tools.Bytes2Chars(TextAsByte)
Debug.Print(New String(TextAsChars)) ' Hello World!

' Sample for BytesToUShorts
Dim sdata() As UShort = Tools.BytesToUShorts({&H0, &H40, &H12, &H34, &H43, &H72, &H0, &H13, &H21, &H0}) ' &H40, &H1234, &H4372, &H13, &H2100

' Sample for UShortsToBytes
Dim bdata() As Byte = Tools.UShortsToBytes({&H40, &H1234, &H4372, &H13, &H2100}) ' &H0, &H40, &H12, &H34, &H43, &H72, &H0, &H13, &H21, &H0

' Sample for ColorToRgb
Dim rgb() As Integer = Tools.ColorToRgb(&HFF0000)
Debug.Print("Red: " + rgb(0).ToString() + " / Green: " + rgb(1).ToString() + " / Blue: " + rgb(2).ToString()) ' Red: 255 / Green: 0 / Blue: 0

' Sample for Dec2Hex
Debug.Print(Tools.Dec2Hex(1234)) ' 4D2

' Sample for Hex2Dec
Debug.Print(Tools.Hex2Dec("ff").ToString()) ' 255

' Sample for Escape
Debug.Print(Tools.Escape("First line" + Constants.vbCrLf + "Second line" + Constants.vbCrLf + "etc.")) ' First line\r\nSecond line\r\netc.

' Sample for MetricPrefix
Debug.Print(Tools.MetricPrefix(3000)) ' 3k

' Sample for RawUrlEncode
Debug.Print(Tools.RawUrlEncode("http:'www.netduino.com/")) ' http%310%215%215www.netduino.com%215

' Sample for RawUrlDecode
Debug.Print(Tools.RawUrlDecode("http%310%215%215www.netduino.com%215")) ' http:'www.netduino.com/

' Sample for Round
Debug.Print(Tools.Round(123.456779F, 2)) ' 123.45

' Sample for XorChecksum
Debug.Print(Tools.XorChecksum("www.netduino.com").ToString()) ' 16

' Sample for ZeroFill
Debug.Print(Tools.ZeroFill(4, 2)) ' 04

Last edited Dec 29, 2012 at 7:46 AM by Garrcomm, version 13