Csharp/C Sharp by API/System/BitConverter — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

BitConverter.GetBytes

  

using System;
using System.Net;
using System.Text;
public class BinaryDataTest
{
   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);
   }
}


BitConverter.ToBoolean

  

using System;
using System.IO;
class MainClass
{
    public static byte[] DecimalToByteArray (decimal src){
        using (MemoryStream stream = new MemoryStream()) {
           using (BinaryWriter writer = new BinaryWriter(stream)){
               writer.Write(src);
               return stream.ToArray();
           }
        }
    }
    public static decimal ByteArrayToDecimal (byte[] src){
        using (MemoryStream stream = new MemoryStream(src)){
           using (BinaryReader reader = new BinaryReader(stream)){
              return reader.ReadDecimal();
           }
        }
    }
    public static void Main()
    {
        byte[] b = null;
        b = BitConverter.GetBytes(true);
        Console.WriteLine(BitConverter.ToString(b));
        Console.WriteLine(BitConverter.ToBoolean(b, 0));
        b = BitConverter.GetBytes(3678);
        Console.WriteLine(BitConverter.ToString(b));
        Console.WriteLine(BitConverter.ToInt32(b, 0));
        b = DecimalToByteArray(285998345545.563846696m);
        Console.WriteLine(BitConverter.ToString(b));
        Console.WriteLine(ByteArrayToDecimal(b));
    }
    
}


BitConverter.ToInt32

  

using System;
using System.IO;
class MainClass
{
    public static byte[] DecimalToByteArray (decimal src){
        using (MemoryStream stream = new MemoryStream()) {
           using (BinaryWriter writer = new BinaryWriter(stream)){
               writer.Write(src);
               return stream.ToArray();
           }
        }
    }
    public static decimal ByteArrayToDecimal (byte[] src){
        using (MemoryStream stream = new MemoryStream(src)){
           using (BinaryReader reader = new BinaryReader(stream)){
              return reader.ReadDecimal();
           }
        }
    }
    public static void Main()
    {
        byte[] b = null;
        b = BitConverter.GetBytes(true);
        Console.WriteLine(BitConverter.ToString(b));
        Console.WriteLine(BitConverter.ToBoolean(b, 0));
        b = BitConverter.GetBytes(3678);
        Console.WriteLine(BitConverter.ToString(b));
        Console.WriteLine(BitConverter.ToInt32(b, 0));
        b = DecimalToByteArray(285998345545.563846696m);
        Console.WriteLine(BitConverter.ToString(b));
        Console.WriteLine(ByteArrayToDecimal(b));
    }
    
}


BitConverter.ToString

  

using System;
using System.IO;
class MainClass
{
    public static byte[] DecimalToByteArray (decimal src){
        using (MemoryStream stream = new MemoryStream()) {
           using (BinaryWriter writer = new BinaryWriter(stream)){
               writer.Write(src);
               return stream.ToArray();
           }
        }
    }
    public static decimal ByteArrayToDecimal (byte[] src){
        using (MemoryStream stream = new MemoryStream(src)){
           using (BinaryReader reader = new BinaryReader(stream)){
              return reader.ReadDecimal();
           }
        }
    }
    public static void Main()
    {
        byte[] b = null;
        b = BitConverter.GetBytes(true);
        Console.WriteLine(BitConverter.ToString(b));
        Console.WriteLine(BitConverter.ToBoolean(b, 0));
        b = BitConverter.GetBytes(3678);
        Console.WriteLine(BitConverter.ToString(b));
        Console.WriteLine(BitConverter.ToInt32(b, 0));
        b = DecimalToByteArray(285998345545.563846696m);
        Console.WriteLine(BitConverter.ToString(b));
        Console.WriteLine(ByteArrayToDecimal(b));
    }
    
}