How does BitConverter.ToInt32 work?

c# convert byte array to int array
c# bitconverter big endian
byte array to decimal c#
c# byte array
bitconverter.toint32 online
bitconverter online
bitconverter java
bitconverter.tostring c#

Here is a method -

using System;

class Program
{
    static void Main(string[] args)
    {
        //
        // Create an array of four bytes.
        // ... Then convert it into an integer and unsigned integer.
        //
        byte[] array = new byte[4];
        array[0] = 1; // Lowest
        array[1] = 64;
        array[2] = 0;
        array[3] = 0; // Sign bit
        //
        // Use BitConverter to convert the bytes to an int and a uint.
        // ... The int and uint can have different values if the sign bit differs.
        //
        int result1 = BitConverter.ToInt32(array, 0); // Start at first index
        uint result2 = BitConverter.ToUInt32(array, 0); // First index
        Console.WriteLine(result1);
        Console.WriteLine(result2);
        Console.ReadLine();
    }
}

Output

16385 16385

I just want to know how this is happening?

The docs for BitConverter.ToInt32 actually have some pretty good examples. Assuming BitConverter.IsLittleEndian returns true, array[0] is the least significant byte, as you've shown... although array[3] isn't just the sign bit, it's the most significant byte which includes the sign bit (as bit 7) but the rest of the bits are for magnitude.

So in your case, the least significant byte is 1, and the next byte is 64 - so the result is:

( 1 * (1 << 0) ) +    // Bottom 8 bits
(64 * (1 << 8) ) +    // Next 8 bits, i.e. multiply by 256
( 0 * (1 << 16)) +    // Next 8 bits, i.e. multiply by 65,536
( 0 * (1 << 24))      // Top 7 bits and sign bit, multiply by 16,777,216

which is 16385. If the sign bit were set, you'd need to consider the two cases differently, but in this case it's simple.

How does BitConverter.ToInt32 work?, BitConverter.ToInt32(Byte[], Int32) Method is used to return a 32-bit signed integer converted from four bytes at a specified position in a byte array  The ToInt32 method converts the bytes from index startIndex to startIndex + 3 to an Int32 value. The order of bytes in the array must reflect the endianness of the computer system's architecture; for more information, see the Remarks section of BitConverter.

It converts like it was a number in base 256. So in your case : 1+64*256 = 16385

C#, The BitConverter.ToInt32() method in C# is used to return a 32-bit signed integer converted from four bytes at a specified position in a byte  Console.WriteLine(BitConverter.ToString(bytes)) If BitConverter.IsLittleEndian Then Array.Reverse(bytes) End If Console.WriteLine(BitConverter.ToString(bytes)) Dim result As Integer = BitConverter.ToInt32(bytes, 0) Console.WriteLine("Original value: {0}", value) Console.WriteLine("Returned value: {0}", result) End Sub End Module ' The example displays the following output on a little-endian system: ' 4E-61-BC-00 ' 00-BC-61-4E ' 00-BC-61-4E ' 4E-61-BC-00 ' Original value: 12345678 ' Returned

Looking at the .Net 4.0 Framework reference source, BitConverter does work how Jon's answer said, though it uses pointers (unsafe code) to work with the array.

However, if the second argument (i.e., startindex) is divisible by 4 (as is the case in your example), the framework takes a shortcut. It takes a byte pointer to the value[startindex], casts it to an int pointer, then dereferences it. This trick works regardless of whether IsLittleEndian is true.

From a high level, this basically just means the code is pointing at 4 bytes in the byte array and categorically declaring, "the chunk of memory over there is an int!" (and then returning a copy of it). This makes perfect sense when you take into account that under the hood, an int is just a chunk of memory.

Below is the source code of the framework ToUint32 method:

return (uint)ToInt32(value, startIndex);

BitConverter.ToInt32() Method in C#, The int and uint can have different values if the sign bit differs. // int result1 = BitConverter.ToInt32(array, 0); // Start at first index uint result2 = BitConverter. The BitConverter class is available in all CLI languages, including VB.NET. The BitConverter methods use bitwise operators to read from byte arrays and return value types such as integers. Example: The ToInt32 method uses the shift << on each byte and the bitwise "|" to combine those shifted values. Shift Bitwise Or

array[0] = 1; // Lowest // 0x01 array[1] = 64; // 0x40 array[2] = 0; // 0x00 array[3] = 0; // Sign bit 0x00

If you combine each hex value 0x00004001

The MSDN documentatin explains everything

How does BitConverter.ToInt32 work? - c# - csharp, To start, this program shows how to use the BitConverter class and its static methods ToInt32 and ToUInt32. These methods convert the byte values stores in a  Using the ToInt32 (String) method is equivalent to passing value to the Int32.Parse (String) method. value is interpreted by using the formatting conventions of the current thread culture. If you prefer not to handle an exception if the conversion fails, you can call the Int32.TryParse method instead.

You can look for yourself - https://referencesource.microsoft.com/#mscorlib/system/bitconverter.cs,e8230d40857425ba

If the data is word-aligned, it will simply cast the memory pointer to an int32.

return *((int *) pbyte);

Otherwise, it uses bitwise logic from the byte memory pointer values.

C# BitConverter Examples, The int and uint can have different values if the sign bit differs. // int result1 = BitConverter.ToInt32(array, 0); // Start at first index uint result2 = BitConverter. The documentation on BitConverter.ToInt32 says: The ToInt32 method converts the bytes from index startIndex to startIndex + 3 to an Int32 value. You need to specify at least 4 bytes, but you only have 2.

How does BitConverter.ToInt32 work? - c#, As the logical operations don't work for non-integral types this is The BitConverter has a GetBytes method that will convert any of the standard  I am getting data as a big-endian but I want it as a little-endian and I am using Bitconverter.toInt32 method for converting that value to integer. My problem is that before converting the value, I have to copy that 4 byte data into temporary array from source byte array and then reverse that temporary byte array.

C# Bit Bashing - The BitConverter, And it does so while still providing safe access with performance you can pass it off to methods that operate on grouped bytes as ints set an plain integer and convert it to an byte array int number = 42; byte[] numberBytes = BitConverter.​GetBytes(number); // now through the implicit casting convert to a  C# | BitConverter.ToUInt32 Method This method is used to return a 32-bit unsigned integer converted from four bytes at a specified position in a byte array. Syntax:

Span - byte to int conversions, has changed since the early work so I wanted to update my example from before and The action of converting is now performed by the MemoryMarshal class convert it to a byte array int number = 42; byte[] numberBytes = BitConverter.GetBytes(number); // now through the implicit casting convert to a  BitConverter.ToInt32 (Byte [], Int32) Method is used to return a 32-bit signed integer converted from four bytes at a specified position in a byte array.

Comments
  • (tries to resist an urge to replace + with | ;p)
  • @Marc: Yeah, I wasn't sure... I felt that if the OP is asking about this, they're probably more comfortable with +.
  • That are the bits I was interested in. Thanks!
  • BitConverter already takes architecture Endianness into account. If you do want to provide a ToIntxx function that accepts an Endianness argument independent of the current architecture, my recommendation would be to replace BitConverter.IsLittleEndian with BitConverter.IsLittleEndian == isLittleEndian, where isLittleEndian is an argument passed to the wrapper function. Also, I find it really dirty that you are checking BitConverter.IsLittleEndian in both your if statement and in the ternary operator. Either use an if statement or a ternary operator but not both.
  • If your concern was some sort of race condition within BitConverter.IsLittleEndian, your concern is misplaced. Firstly, your solution doesn't resolve such a race condition (though it does narrow it). Secondly, BitConverter.IsLittleEndian indicates architecture Endianness. The value of IsLittleEndian is set at compile time of the framework.