Convert byte array to Int32 : BitConverter « Development Class « C# / C Sharp






Convert byte array to Int32

 

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));
    }
    
}   
           
         
  








Related examples in the same category

1.Convert byte array to String with BitConverter
2.BitConverter converts base data types to an array of bytes, and an array of bytes to base data types.
3.Convert bytes to UInt32.
4.Convert various data type to string with BitConverter
5.Converts double number to a 64-bit signed integer.
6.Convert Boolean value to an array of bytes.
7.Convert Unicode character to an array of bytes.
8.Convert double to an array of bytes.
9.Convert 16-bit signed integer value to an array of bytes.
10.Convert 32-bit signed integer value to an array of bytes.
11.Convert 64-bit signed integer value to an array of bytes.
12.Convert single-precision floating point value to an array of bytes.
13.Convert specified 16-bit unsigned integer value to an array of bytes.
14.Convert specified 32-bit unsigned integer value to an array of bytes.
15.Convert specified 64-bit unsigned integer value to an array of bytes.
16.Converts 64-bit signed integer to a double-precision floating point number.
17.Indicates the byte order ("endianness")
18.Convert one byte at a specified position in a byte array to bool
19.Convert two bytes at a specified position in a byte array to a Unicode character
20.Convert two bytes at a specified position in a byte array to a 16-bit signed integer
21.Convert four bytes at a specified position in a byte array to a 32-bit signed integer