ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

Convert Byte Array to Int in C#

.Net C# about 7 years ago || 11/29/2018 || 13.8 K View

  • Hide

In .NET Framework, BitConverter Class helps us to convert base Datatypes to an array of bytes and vice-versa.

GetBytes Method that takes an integer, double or other base type value is static overloaded method of BitConverter class which converts that to an array of bytes. To reverse this conversion BitConverter also has other static methods. For Example:

  • ToDouble,
  • ToChart,
  • ToBoolean,
  • ToInt16,
  • ToSingle.

Code snippet below converts different integer values to a byte array and vice-versa.

namespace BitConverterExample {
 class Program {
  static void Main(string[] args) {
   Console.WriteLine("Convert Byte Array to Int in C# and vice-versa");
   // Create double to a byte array  
   Int32 yourInput = 235;
   Console.WriteLine("Your Int value is : " + yourInput.ToString());
   byte[] bytes = ConvertInt32ToByteArray(yourInput);
   Console.WriteLine("Your Byte array value:");
   Console.WriteLine(BitConverter.ToString(bytes));
   Console.WriteLine("Your Byte array back to Int32:");
   // Create byte array to Int32  
   double dValue = ConvertByteArrayToInt32(bytes);
   Console.WriteLine(dValue.ToString());
   Console.ReadLine();
  }
  public static byte[] ConvertInt32ToByteArray(Int32 param) {
   return BitConverter.GetBytes(param);
  }
  public static byte[] ConvertIntToByteArray(Int16 param) {
   return BitConverter.GetBytes(param);
  }
  public static byte[] ConvertIntToByteArray(Int64 param) {
   return BitConverter.GetBytes(param);
  }
  public static byte[] ConvertIntToByteArray(int param) {
   return BitConverter.GetBytes(param);
  }
  public static double ConvertByteArrayToInt32(byte[] param) {
   return BitConverter.ToInt32(param, 0);
  }
 }
}

OUTPUT: 

 

  • 0
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Anthony M. Healey

Anthony M. Healey

Touch of Creativity and a bit of active on brain power

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey