Converts base data types to an array of bytes, and an array of bytes to base data types.
Public NotInheritable Class BitConverter
You do not need to declare an instance of a static class in order to access its members.
public static class BitConverter
public ref class BitConverter abstract sealed
public final class BitConverter
This class facilitates manipulating value types in their fundamental form. A byte is defined as an 8-bit unsigned integer.
The following code example illustrates the use of several BitConverter class methods.
' Example of BitConverter class methods. Module BitConverterDemo Sub Main( ) Const formatter As String = "{0,25}{1,30}" Dim aDoubl As Double = 0.1111111111111111111 Dim aSingl As Single = 0.1111111111111111111 Dim aLong As Long = 1111111111111111111 Dim anInt As Integer = 1111111111 Dim aShort As Short = 11111 Dim aChar As Char = "*"c Dim aBool As Boolean = True Console.WriteLine( _ "This example of methods of the BitConverter class" & _ vbCrLf & "generates the following output." & vbCrLf ) Console.WriteLine( formatter, "argument", "Byte array" ) Console.WriteLine( formatter, "--------", "----------" ) ' Convert values to Byte arrays and display them. Console.WriteLine( formatter, aDoubl, _ BitConverter.ToString( BitConverter.GetBytes( aDoubl ) ) ) Console.WriteLine( formatter, aSingl, _ BitConverter.ToString( BitConverter.GetBytes( aSingl ) ) ) Console.WriteLine( formatter, aLong, _ BitConverter.ToString( BitConverter.GetBytes( aLong ) ) ) Console.WriteLine( formatter, anInt, _ BitConverter.ToString( BitConverter.GetBytes( anInt ) ) ) Console.WriteLine( formatter, aShort, _ BitConverter.ToString( BitConverter.GetBytes( aShort ) ) ) Console.WriteLine( formatter, aChar, _ BitConverter.ToString( BitConverter.GetBytes( aChar ) ) ) Console.WriteLine( formatter, aBool, _ BitConverter.ToString( BitConverter.GetBytes( aBool ) ) ) End Sub End Module ' This example of methods of the BitConverter class ' generates the following output. ' ' argument Byte array ' -------- ---------- ' 0.111111111111111 1C-C7-71-1C-C7-71-BC-3F ' 0.1111111 39-8E-E3-3D ' 1111111111111111111 C7-71-C4-2B-AB-75-6B-0F ' 1111111111 C7-35-3A-42 ' 11111 67-2B ' * 2A-00 ' True 01
// Example of BitConverter class methods. using System; class BitConverterDemo { public static void Main( ) { const string formatter = "{0,25}{1,30}"; double aDoubl = 0.1111111111111111111; float aSingl = 0.1111111111111111111F; long aLong = 1111111111111111111; int anInt = 1111111111; short aShort = 11111; char aChar = '*'; bool aBool = true; Console.WriteLine( "This example of methods of the BitConverter class" + "\ngenerates the following output.\n" ); Console.WriteLine( formatter, "argument", "byte array" ); Console.WriteLine( formatter, "--------", "----------" ); // Convert values to Byte arrays and display them. Console.WriteLine( formatter, aDoubl, BitConverter.ToString( BitConverter.GetBytes( aDoubl ) ) ); Console.WriteLine( formatter, aSingl, BitConverter.ToString( BitConverter.GetBytes( aSingl ) ) ); Console.WriteLine( formatter, aLong, BitConverter.ToString( BitConverter.GetBytes( aLong ) ) ); Console.WriteLine( formatter, anInt, BitConverter.ToString( BitConverter.GetBytes( anInt ) ) ); Console.WriteLine( formatter, aShort, BitConverter.ToString( BitConverter.GetBytes( aShort ) ) ); Console.WriteLine( formatter, aChar, BitConverter.ToString( BitConverter.GetBytes( aChar ) ) ); Console.WriteLine( formatter, aBool, BitConverter.ToString( BitConverter.GetBytes( aBool ) ) ); } } /* This example of methods of the BitConverter class generates the following output. argument byte array -------- ---------- 0.111111111111111 1C-C7-71-1C-C7-71-BC-3F 0.1111111 39-8E-E3-3D 1111111111111111111 C7-71-C4-2B-AB-75-6B-0F 1111111111 C7-35-3A-42 11111 67-2B * 2A-00 True 01 */
// Example of BitConverter class methods. using namespace System; int main() { String^ formatter = "{0,25}{1,30}"; double aDoubl = 0.1111111111111111111; float aSingl = 0.1111111111111111111F; __int64 aLong = 1111111111111111111; int anInt = 1111111111; short aShort = 11111; __wchar_t aChar = L'*'; bool aBool = true; Console::WriteLine( "This example of methods of the BitConverter class" "\ngenerates the following output.\n" ); Console::WriteLine( formatter, "argument", "byte array" ); Console::WriteLine( formatter, "--------", "----------" ); // Convert values to Byte arrays and display them. Console::WriteLine( formatter, aDoubl, BitConverter::ToString( BitConverter::GetBytes( aDoubl ) ) ); Console::WriteLine( formatter, aSingl, BitConverter::ToString( BitConverter::GetBytes( aSingl ) ) ); Console::WriteLine( formatter, aLong, BitConverter::ToString( BitConverter::GetBytes( aLong ) ) ); Console::WriteLine( formatter, anInt, BitConverter::ToString( BitConverter::GetBytes( anInt ) ) ); Console::WriteLine( formatter, aShort, BitConverter::ToString( BitConverter::GetBytes( aShort ) ) ); Console::WriteLine( formatter, aChar, BitConverter::ToString( BitConverter::GetBytes( aChar ) ) ); Console::WriteLine( formatter, aBool, BitConverter::ToString( BitConverter::GetBytes( aBool ) ) ); } /* This example of methods of the BitConverter class generates the following output. argument byte array -------- ---------- 0.111111111111111 1C-C7-71-1C-C7-71-BC-3F 0.1111111 39-8E-E3-3D 1111111111111111111 C7-71-C4-2B-AB-75-6B-0F 1111111111 C7-35-3A-42 11111 67-2B * 2A-00 True 01 */
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
say I want to send data to another app and have it use that data...
so I have an array of bytes formated by bitconverter to "01-FF-01-FF"
now the reciving app needs to get that back into an array of 4 bytes.
seems like BitCOnverter should have an OP for StringToBytes( string input) that returns an array of bytes.
then we have a round trip conversion.
The System.BitConverter class allows you to convert between bytes (in an array) and numerical types (int, uint, etc). However, it doesn't seem to let you set the endianness (which byte is most significant, e.g. in an int/Int32, there are four bytes, so which way do you read them?). It seems to operate such that the methods always return values appropriate to little endian (host order) systems (most significant byte is on the right). Since, in embedded systems, it is often necessary to interact with big-endian systems, I created this class to provide the functionality for either endianness. You simply set the endianness in the class, then all methods will return endian-aware values.
Please let me know if there is a better solution out there, as I searched a lot before deciding to do it this way.