https://github.com/aramzham/byte-array-to-int32-converter
Convert byte array to Int32 on C# 6.0
https://github.com/aramzham/byte-array-to-int32-converter
converter csharp-code
Last synced: 3 months ago
JSON representation
Convert byte array to Int32 on C# 6.0
- Host: GitHub
- URL: https://github.com/aramzham/byte-array-to-int32-converter
- Owner: aramzham
- Created: 2017-01-15T14:48:06.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-20T16:23:59.000Z (over 8 years ago)
- Last Synced: 2025-03-24T01:11:11.329Z (3 months ago)
- Topics: converter, csharp-code
- Language: C#
- Size: 6.84 KB
- Stars: 15
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Byte-array-to-Int32-converter
Convert byte array to Int32 on C# 6.0This method takes a byte array and converts it to an Int32.
Here is the body of the method:```javascript
public static int ConvertByteArrayToInt32(byte[] bytes)
{
var binary = bytes.Reverse().Select(x => Convert.ToString(x, 2).PadLeft(8, '0')).SelectMany(x => x).ToArray();var result = 0;
for (int i = 0; i < binary.Length; i++)
if (binary[binary.Length - i - 1] == '1') result += (int)Math.Pow(2, i);return result;
}
```After measuring the performances of `BitConverter` from `System.Diagnostics`, my custom converter and @tigranv 's `FromByteToInt1`, the outcome reveals that you **don't need to reinvent the wheel**. Everything you need exists already in the language, you just need to find its place. :+1:
![]()