Int to byte array converter : Byte Array « File Stream « C# / C Sharp






Int to byte array converter

       
using System;
using System.Collections.Generic;
using System.Text;

namespace ZWaveApi.Net.Utilities
{


    static class ByteFunctions
    {

        private const byte _sizeMask = 0x07;
        private const byte _scaleMask = 0x18;
        private const byte _scaleShift = 0x03;
        private const byte _precisionMask = 0xe0;
        private const byte _precisionShift = 0x05;

        static public int ExtractValue(byte[] _data, ref byte _scale)
        {
            byte size = (byte)(_data[0] & _sizeMask);
            byte precision = (byte)((_data[0] & _precisionMask) >> _precisionShift);

            if (_scale != null)
            {
                _scale = (byte)((_data[0] & _scaleMask) >> _scaleShift);
            }

            UInt32 value = 0;
            int result = 0;
            byte i;
            for (i = 0; i < size; ++i)
            {
                value <<= 8;
                value |= (UInt32)_data[i + 1];
            }


            // Deal with sign extension.  All values are signed
            if ((_data[1] & 0x80) == 0x80)
            {

                // MSB is signed
                if (size == 1)
                {
                    value |= 0xffffff00;
                }
                else if (size == 2)
                {
                    value |= 0xffff0000;
                }
                result = (int)(-1 * value);
            }
            else
            {
                result = (int)(value);
            }


            if (precision == 0)
            {
                return result;
            }
            else
            {
                return result / (int)Math.Pow(10, precision);
            }

        }


        static public int ByteToInt(byte[] _value)
        {

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(_value);
            }
            if (_value.Length == 1)
            {
                return (Byte)(_value[0]);
            }
            else if (_value.Length == 2)
            {
                return BitConverter.ToInt16(_value, 0);
            }
            else
            {
                return BitConverter.ToInt32(_value, 0);
            }


        }

        static public byte[] IntToBytes(int _value)
        {

            byte[] result;

            int size = 4;

            if (_value < 0)
            {
                if ((_value & 0xffffff80) == 0xffffff80)
                {
                    size = 1;
                }
                else if ((_value & 0xffff8000) == 0xffff8000)
                {
                    size = 2;
                }
            }
            else
            {
                if ((_value & 0xffffff00) == 0)
                {
                    size = 1;
                }
                else if ((_value & 0xffff0000) == 0)
                {
                    size = 2;
                }
            }

            result = new byte[size];

            if (size > 2)
            {
                result[size - 4] = (byte)(_value >> 24);
                result[size - 3] = (byte)(_value >> 16);
            }
            if (size > 1)
            {
                result[size - 2] = (byte)(_value >> 8);
            }
            result[size - 1] = (byte)(_value);

            return result;

        }
    }
}

   
    
    
    
    
    
    
  








Related examples in the same category

1.Convert a byte array to string using default encoding
2.Get the byte array from a string using default encoding
3.Write a byte array to a file
4.Read byte array from file
5.Read Stream to Byte array
6.Save Byte Array To File
7.Reinitializes a byte array to the given value in an optimized way: byteArraySet
8.Bit Array To Byte Array
9.Bit Array To Byte Array (2)
10.Checks if the input byte array contains only safe values, the data does not need to be encoded for use with LDIF
11.Convert byte array into a string
12.Byte Array To String
13.Bytes To Formatted String
14.Get string from byte array
15.Reads a file into a byte array
16.Read entire stream into a byte array
17.Read Stream to fill the byte array
18.Convert to easy-to-read byte value
19.Conversions between complex types and byte arrays.
20.Search a byte array for a sub byte array
21.Append various data types to byte array
22.Convert a byte array to an Object.
23.Compares the values of two byte arrays, and returns true only if every array member is identical
24.Convert an object to a byte array.
25.Bytes Equal
26.Compare Bytes
27.Pad bytes.
28.Bytes To Comma Separated List
29.Compare Byte Array Elements
30.Byte Arrays Are Equal
31.Converts a number of bytes into a more easily read form.
32.Compares up to n elements of byte array a[] against byte array b[].
33.Utility method to compare two byte arrays for equality
34.Swap Byte Order
35.Get bytes and read bytes
36.Copy as much data as possible for a read operation.