Csharp/CSharp Tutorial/Development/BitConverter
Содержание
BitConverter
Convert a built-in data type into an array of bytes.
<source lang="csharp">public static readonly bool IsLittleEndian</source>
BitConverter is sealed, which means that it cannot be inherited.
Methods Defined by BitConverter
Method Meaning public static longDoubleToInt64Bits(double v) Converts v into a long integer. public static byte[ ] GetBytes(bool v) Converts v into a 1-byte array. public static byte[ ] GetBytes(char v) Converts v into a 2-byte array. public static byte[ ] GetBytes(double v) Converts v into an 8-byte. public static byte[ ] GetBytes(float v) Converts v into a 4-byte. public static byte[ ] GetBytes(int v) Converts v into a 4-byte. public static byte[ ] GetBytes(long v) Converts v into an 8-byte. public static byte[ ] GetBytes(short v) Converts v into a 2-byte. public static byte[ ] GetBytes(uint v) Converts v into a 4-byte. public static byte[ ] GetBytes(ulong v) Converts v into an 8-byte. public static byte[ ] GetBytes(ushort v) Converts v into a 2-byte. public static doubleInt64BitsToDouble(long v) Converts v into a double floating-point value. public static bool ToBoolean(byte[ ] a,int idx) Converts the byte at a[idx] into its bool equivalent. A non-zero value is converted to true; zero is converted to false. public static char ToChar(byte[ ] a,int start) Converts two bytes starting at a[start] into its char equivalent. public static double ToDouble(byte[ ] a,int start) Converts eight bytes starting at a[start] into its double equivalent. public static short ToInt16(byte[ ] a,int start) Converts two bytes starting at a[start] into its short equivalent. public static int ToInt32(byte[ ] a,int start) Converts four bytes starting at a[start] into its int equivalent. public static long ToInt64(byte[ ] a,int start) Converts eight bytes starting at a[start] into its long equivalent. public static float ToSingle(byte[ ] a,int start) Converts four bytes starting at a[start] into its float equivalent. public static string ToString(byte[ ] a) Converts the bytes in a into a string. The string contains the hexadecimal values associated with the bytes, separated by hyphens. public static string ToString(byte[ ] a, int start) Converts the bytes in a, beginning at a[start], into a string. The string contains the hexadecimal values associated with the bytes, separated by hyphens. public static string ToString(byte[ ] a, int start, int count) Converts the bytes in a, beginning at a[start] and running for count bytes, into a string. The string contains the hexadecimal values associated with the bytes, separated by hyphens. public static ushort ToUInt16(byte[ ] a,int start) Converts two bytes starting at a[start] into its ushort equivalent and returns the result. public static uint ToUInt32(byte[ ] a,int start) Converts four bytes starting at a[start] into its uint equivalent and returns the result. public static ulong ToUInt64(byte[ ] a,int start) Converts eight bytes starting at a[start] into its ulong equivalent and returns the result.
Convert a bool to a byte array and display
<source lang="csharp">using System; using System.IO;
class MainClass {
public static void Main() { byte[] b = null; b = BitConverter.GetBytes(true); Console.WriteLine(BitConverter.ToString(b)); }
}</source>
01
Convert a byte array to a bool and display
<source lang="csharp">using System; using System.IO;
class MainClass {
public static void Main() { byte[] b = null; b = BitConverter.GetBytes(true); Console.WriteLine(BitConverter.ToBoolean(b,0));
}
}</source>
True
Convert a byte array to an int and display
<source lang="csharp">using System; using System.IO;
class MainClass {
public static void Main() { byte[] b = null; b = BitConverter.GetBytes(3678); Console.WriteLine(BitConverter.ToInt32(b,0)); }
}</source>
3678
Convert an int to a byte array and display
<source lang="csharp">using System; using System.IO;
class MainClass {
public static void Main() { byte[] b = null; b = BitConverter.GetBytes(3678); Console.WriteLine(BitConverter.ToString(b)); }
}</source>
5E-0E-00-00
Convert different data types to byte array and convert byte array back
<source lang="csharp">using System; using System.Net; using System.Text; class MainClass {
public static void Main() { int test1 = 45; double test2 = 3.14159; int test3 = -1234567890; bool test4 = false; byte[] data = new byte[1024]; string output; data = BitConverter.GetBytes(test1); output = BitConverter.ToString(data); Console.WriteLine("test1 = {0}, string = {1}", test1, output); data = BitConverter.GetBytes(test2); output = BitConverter.ToString(data); Console.WriteLine("test2 = {0}, string = {1}", test2, output); data = BitConverter.GetBytes(test3); output = BitConverter.ToString(data); Console.WriteLine("test3 = {0}, string = {1}", test3, output); data = BitConverter.GetBytes(test4); output = BitConverter.ToString(data); Console.WriteLine("test4 = {0}, string = {1}", test4, output); }
}</source>
test1 = 45, string = 2D-00-00-00 test2 = 3.14159, string = 6E-86-1B-F0-F9-21-09-40 test3 = -1234567890, string = 2E-FD-69-B6 test4 = False, string = 00