Catfood.Shapefile.EndianBitConverter.ToInt32 C# (CSharp) Method

ToInt32() public static method

Returns an integer from four bytes of a byte array
Thrown if value is null Thrown if startIndex is invalid
public static ToInt32 ( byte value, int startIndex, ProvidedOrder order ) : int
value byte bytes to convert
startIndex int start index in value
order ProvidedOrder byte order of value
return int
        public static int ToInt32(byte[] value, int startIndex, ProvidedOrder order)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if ((startIndex + sizeof(int)) > value.Length)
            {
                throw new ArgumentException("startIndex invalid (not enough space in value to extract an integer", "startIndex");
            }

            if (BitConverter.IsLittleEndian && (order == ProvidedOrder.Big))
            {
                byte[] toConvert = new byte[sizeof(int)];
                Array.Copy(value, startIndex, toConvert, 0, sizeof(int));
                Array.Reverse(toConvert);
                return BitConverter.ToInt32(toConvert, 0);
            }
            else
            {
                return BitConverter.ToInt32(value, startIndex);
            }
        }

Usage Example

Example #1
0
        /// <summary>
        /// The header data for a Shapefile main file or Index file
        /// </summary>
        /// <param name="headerBytes">The first 100 bytes of the Shapefile main file or Index file</param>
        /// <exception cref="ArgumentNullException">Thrown if headerBytes is null</exception>
        /// <exception cref="InvalidOperationException">Thrown if an error occurs parsing the header</exception>
        public Header(byte[] headerBytes)
        {
            if (headerBytes == null)
            {
                throw new ArgumentNullException("headerBytes");
            }

            if (headerBytes.Length != HeaderLength)
            {
                throw new InvalidOperationException(string.Format("headerBytes must be {0} bytes long",
                                                                  HeaderLength));
            }

            //Position  Field           Value       Type        Order
            //Byte 0    File Code       9994        Integer     Big
            //Byte 4    Unused          0           Integer     Big
            //Byte 8    Unused          0           Integer     Big
            //Byte 12   Unused          0           Integer     Big
            //Byte 16   Unused          0           Integer     Big
            //Byte 20   Unused          0           Integer     Big
            //Byte 24   File Length     File Length Integer     Big
            //Byte 28   Version         1000        Integer     Little
            //Byte 32   Shape Type      Shape Type  Integer     Little
            //Byte 36   Bounding Box    Xmin        Double      Little
            //Byte 44   Bounding Box    Ymin        Double      Little
            //Byte 52   Bounding Box    Xmax        Double      Little
            //Byte 60   Bounding Box    Ymax        Double      Little
            //Byte 68*  Bounding Box    Zmin        Double      Little
            //Byte 76*  Bounding Box    Zmax        Double      Little
            //Byte 84*  Bounding Box    Mmin        Double      Little
            //Byte 92*  Bounding Box    Mmax        Double      Little

            _fileCode = EndianBitConverter.ToInt32(headerBytes, 0, ProvidedOrder.Big);
            if (_fileCode != ExpectedFileCode)
            {
                throw new InvalidOperationException(string.Format("Header File code is {0}, expected {1}",
                                                                  _fileCode,
                                                                  ExpectedFileCode));
            }

            _version = EndianBitConverter.ToInt32(headerBytes, 28, ProvidedOrder.Little);
            if (_version != ExpectedVersion)
            {
                throw new InvalidOperationException(string.Format("Header version is {0}, expected {1}",
                                                                  _version,
                                                                  ExpectedVersion));
            }

            _fileLength = EndianBitConverter.ToInt32(headerBytes, 24, ProvidedOrder.Big);
            _shapeType  = (ShapeType)EndianBitConverter.ToInt32(headerBytes, 32, ProvidedOrder.Little);
            _xMin       = EndianBitConverter.ToDouble(headerBytes, 36, ProvidedOrder.Little);
            _yMin       = EndianBitConverter.ToDouble(headerBytes, 44, ProvidedOrder.Little);
            _xMax       = EndianBitConverter.ToDouble(headerBytes, 52, ProvidedOrder.Little);
            _yMax       = EndianBitConverter.ToDouble(headerBytes, 60, ProvidedOrder.Little);
            _zMin       = EndianBitConverter.ToDouble(headerBytes, 68, ProvidedOrder.Little);
            _zMax       = EndianBitConverter.ToDouble(headerBytes, 76, ProvidedOrder.Little);
            _mMin       = EndianBitConverter.ToDouble(headerBytes, 84, ProvidedOrder.Little);
            _mMax       = EndianBitConverter.ToDouble(headerBytes, 92, ProvidedOrder.Little);
        }
All Usage Examples Of Catfood.Shapefile.EndianBitConverter::ToInt32
EndianBitConverter