|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
BitConverter, classe
Espace de noms : System
Assembly : mscorlib (dans mscorlib.dll)
Le type BitConverter expose les membres suivants.
| Nom | Description | |
|---|---|---|
![]() ![]() ![]() ![]() | DoubleToInt64Bits | |
![]() ![]() ![]() ![]() ![]() | GetBytes(Boolean) | |
![]() ![]() ![]() ![]() ![]() | GetBytes(Char) | |
![]() ![]() ![]() ![]() ![]() | GetBytes(Double) | |
![]() ![]() ![]() ![]() ![]() | GetBytes(Int16) | |
![]() ![]() ![]() ![]() ![]() | GetBytes(Int32) | |
![]() ![]() ![]() ![]() ![]() | GetBytes(Int64) | |
![]() ![]() ![]() ![]() ![]() | GetBytes(Single) | |
![]() ![]() ![]() ![]() ![]() | GetBytes(UInt16) | |
![]() ![]() ![]() ![]() ![]() | GetBytes(UInt32) | |
![]() ![]() ![]() ![]() ![]() | GetBytes(UInt64) | |
![]() ![]() ![]() ![]() | Int64BitsToDouble | |
![]() ![]() ![]() ![]() ![]() | ToBoolean | |
![]() ![]() ![]() ![]() ![]() | ToChar | |
![]() ![]() ![]() ![]() ![]() | ToDouble | |
![]() ![]() ![]() ![]() ![]() | ToInt16 | |
![]() ![]() ![]() ![]() ![]() | ToInt32 | |
![]() ![]() ![]() ![]() ![]() | ToInt64 | |
![]() ![]() ![]() ![]() ![]() | ToSingle | |
![]() ![]() ![]() ![]() ![]() | ToString(Byte[]) | |
![]() ![]() ![]() ![]() ![]() | ToString(Byte[], Int32) | |
![]() ![]() ![]() ![]() ![]() | ToString(Byte[], Int32, Int32) | |
![]() ![]() ![]() ![]() ![]() | ToUInt16 | |
![]() ![]() ![]() ![]() ![]() | ToUInt32 | |
![]() ![]() ![]() ![]() ![]() | ToUInt64 |
| Nom | Description | |
|---|---|---|
![]() ![]() ![]() ![]() ![]() | IsLittleEndian |
using System; public class Example { public static void Main() { int value = -16; Byte[] bytes = BitConverter.GetBytes(value); // Convert bytes back to Int32. int intValue = BitConverter.ToInt32(bytes, 0); Console.WriteLine("{0} = {1}: {2}", value, intValue, value.Equals(intValue) ? "Round-trips" : "Does not round-trip"); // Convert bytes to UInt32. uint uintValue = BitConverter.ToUInt32(bytes, 0); Console.WriteLine("{0} = {1}: {2}", value, uintValue, value.Equals(uintValue) ? "Round-trips" : "Does not round-trip"); } } // The example displays the following output: // -16 = -16: Round-trips // -16 = 4294967280: Does not round-trip
Si tous les systèmes d'envoi et de réception des données ont la garantie d'avoir le même ordre de primauté des octets, les données ne doivent pas être modifiées. Si tous les systèmes d'envoi et de réception des données peuvent avoir un ordre différent de primauté des octets, transmettez toujours les données dans un ordre particulier. Ceci signifie que l'ordre des octets dans le tableau peut devoir être inversé avant leur envoi ou après leur réception. Il convient généralement de transmettre les données dans l'ordre d'octet du réseau (ordre big-endian) L'exemple suivant fournit une implémentation pour l'envoi d'une valeur entière dans l'ordre d'octets du réseau. using System; public class Example { public static void Main() { int value = 12345678; byte[] bytes = BitConverter.GetBytes(value); Console.WriteLine(BitConverter.ToString(bytes)); if (BitConverter.IsLittleEndian) bytes = ReverseBytes(bytes); Console.WriteLine(BitConverter.ToString(bytes)); // Call method to send byte stream across machine boundaries. // Receive byte stream from beyond machine boundaries. Console.WriteLine(BitConverter.ToString(bytes)); if (BitConverter.IsLittleEndian) bytes = ReverseBytes(bytes); Console.WriteLine(BitConverter.ToString(bytes)); int result = BitConverter.ToInt32(bytes, 0); Console.WriteLine("Original value: {0}", value); Console.WriteLine("Returned value: {0}", result); } private static byte[] ReverseBytes(byte[] inArray) { byte temp; int highCtr = inArray.Length - 1; for (int ctr = 0; ctr < inArray.Length / 2; ctr++) { temp = inArray[ctr]; inArray[ctr] = inArray[highCtr]; inArray[highCtr] = temp; highCtr -= 1; } return inArray; } } // The example displays the following output on a little-endian system: // 4E-61-BC-00 // 00-61-BC-4E // 00-61-BC-4E // 4E-61-BC-00 // Original value: 12345678 // Returned value: 12345678
Si les systèmes d'envoi et de réception de données peuvent avoir un ordre différent de primauté des octets et si les données à transmettre consistent en des entiers signés, appelez la méthode IPAddress.HostToNetworkOrder pour convertir les données en ordre d'octets du réseau et la méthode IPAddress.NetworkToHostOrder pour le convertir en ordre requis par le destinataire.
// 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 */
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (rôle principal du serveur non pris en charge), Windows Server 2008 R2 (rôle principal du serveur pris en charge avec SP1 ou version ultérieure ; Itanium non pris en charge)
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.


