User Tools

Site Tools

blog:2023-07-02_c_convert_short_to_byte_array



2023-07-03 C#: Convert Short to Byte Array

  • The BitConverter class in .NET Framework is provides functionality to convert base data types to an array of bytes and an array of bytes to base data types.
  • The BitConverter class has a static overloaded GetBytes method that takes an integer, double, bool, short, long, or other base type value and converts that to an array of bytes. The BitConverter class also has other static methods to reverse this conversion. Some of these methods are ToDouble, ToChart, ToBoolean, ToInt16, and ToSingle.

Code

  • // Online C# Editor for free
    // Write, Edit and Run your C# code using C# Online Compiler
    
    using System;
    
    public class HelloWorld
    {
        public static void Main(string[] args)
        {
            Single _Value = 5.1F;
            byte[] _bytes = BitConverter.GetBytes(_Value);
            Console.WriteLine("Max Torque Out Limit: " + 
                _bytes[3].ToString("X02") +
                _bytes[2].ToString("X02") +
                _bytes[1].ToString("X02") +
                _bytes[0].ToString("X02")
                );
        }
    }

TAGS

  • 83 person(s) visited this page until now.

Permalink blog/2023-07-02_c_convert_short_to_byte_array.txt · Last modified: 2023/07/03 14:32 by jethro

oeffentlich