public void ToXXX()
        {
            var sut = new LittleEndianBitConverter();

            var data = new byte[] { 0x03, 0, 0, 0, 0, 0, 0, 0 };

            Check.That(sut.ToBoolean(data, 0)).IsTrue();
            Check.That(sut.ToBoolean(data, 7)).IsFalse();
            Check.That(sut.ToChar(data, 0)).IsEqualTo('\u0003');
            Check.That(sut.ToChar(data, 6)).IsEqualTo('\0');
            Check.That(sut.ToInt16(data, 0)).IsEqualTo(3);
            Check.That(sut.ToInt16(data, 6)).IsEqualTo(0);
            Check.That(sut.ToUInt16(data, 0)).IsEqualTo(3u);
            Check.That(sut.ToUInt16(data, 6)).IsEqualTo(0u);
            Check.That(sut.ToInt32(data, 0)).IsEqualTo(3);
            Check.That(sut.ToInt32(data, 4)).IsEqualTo(0);
            Check.That(sut.ToUInt32(data, 0)).IsEqualTo(3u);
            Check.That(sut.ToUInt32(data, 4)).IsEqualTo(0u);
            Check.That(sut.ToInt64(data, 0)).IsEqualTo(3L);
            Check.That(sut.ToUInt64(data, 0)).IsEqualTo(3UL);

            data = new byte[] { 0, 0, 0, 0, 0, 0, 0x20, 0x41 };
            Check.That(sut.ToSingle(data, 0)).IsEqualTo(0.0f);
            Check.That(sut.ToSingle(data, 4)).IsEqualTo(10.0f);

            data = new byte[] { 0, 0, 0, 0, 0, 0, 0x24, 0x40 };
            Check.That(sut.ToDouble(data, 0)).IsEqualTo(10.0);
        }
        public short DecodeInt16(byte[] bytes, Iterator it)
        {
            short value = bitConverter.ToInt16(bytes, it.Offset);

            it.Offset += 2;
            return(value);
        }
        public void LittleEndianBitConverter_Int16()
        {
            const int n = sizeof(Int16);

            const Int16 pos  = 0x1122;
            const Int16 zero = 0;
            const Int16 neg  = -pos;

            var posRepr = new byte[] { 0x22, 0x11 };

            // --------------------------------------------------------------

            var buffer = new byte[n];

            LittleEndianBitConverter.FillBytes(pos, buffer);
            Assert.IsTrue(buffer.SequenceEqual(posRepr));
            Assert.IsTrue(LittleEndianBitConverter.GetBytes(pos).SequenceEqual(buffer));
            Assert.AreEqual(pos, LittleEndianBitConverter.ToInt16(buffer));

            LittleEndianBitConverter.FillBytes(zero, buffer);
            Assert.IsTrue(buffer.SequenceEqual(new byte[n]));
            Assert.IsTrue(LittleEndianBitConverter.GetBytes(zero).SequenceEqual(buffer));
            Assert.AreEqual(zero, LittleEndianBitConverter.ToInt16(buffer));

            LittleEndianBitConverter.FillBytes(neg, buffer);
            Assert.IsTrue(buffer.SequenceEqual(Neg(posRepr)));
            Assert.IsTrue(LittleEndianBitConverter.GetBytes(neg).SequenceEqual(buffer));
            Assert.AreEqual(neg, LittleEndianBitConverter.ToInt16(buffer));
        }
Exemple #4
0
        public unsafe short imethod_45()
        {
            short int16;

            fixed(byte *pointer = this.byte_5)
            {
                this.method_0(pointer);
                int16 = LittleEndianBitConverter.ToInt16(this.byte_5);
            }

            return(int16);
        }
Exemple #5
0
        public unsafe short imethod_14()
        {
            short num;

            switch (this.imethod_13())
            {
            case 0:
                fixed(byte *pointer = this.byte_5)
                {
                    this.method_0(pointer);
                    num = LittleEndianBitConverter.ToInt16(this.byte_5);
                }

                break;

            case 1:
                if (this.int_0 == 0)
                {
                    this.method_6();
                    num = (short)this.byte_3;
                    break;
                }
                num = (short)this.method_7();
                break;

            case 2:
                num = (short)0;
                break;

            case 3:
                num = (short)256;
                break;

            default:
                throw new DxfException("Illegal short.");
            }
            return(num);
        }
Exemple #6
0
 public static short smethod_3(Stream stream)
 {
     byte[] numArray = new byte[2];
     stream.Read(numArray, 0, 2);
     return(LittleEndianBitConverter.ToInt16(numArray, 0));
 }
Exemple #7
0
        /// <summary>
        /// Read all metadata (headers, encodings, formats etc...)
        /// </summary>
        /// <param name="fileInfo">The file info of the sgy file</param>
        /// <returns></returns>
        public static SgyFile Open(FileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                throw new ArgumentNullException("File info cannot be null");
            }
            if (fileInfo.Exists == false)
            {
                throw new FileNotFoundException($"File {fileInfo.FullName} does not exist");
            }
            if (fileInfo.Length < TextHeaderBytesCount + BinaryHeaderBytesCount)
            {
                throw new ArgumentException($"File {fileInfo.FullName} does not have enough bytes in its content to infer file metadata.  This implies the file is corrupt, or not a real sgy file. Try creating a new sgy file isntead.");
            }

            // Local variables
            byte[]             buffer = new byte[65536];
            FileStream         fileStream;
            Encoding           textHeaderEncoding;
            List <string>      textHeaders = new List <string>();
            EndianBitConverter endianBitConverter;
            FileHeader         binaryHeader;
            EndianBitConverter bigEBitConverter = new BigEndianBitConverter();
            EndianBitConverter lilEBitConverter = new LittleEndianBitConverter();

            // Get text header bytes and binary header bytes
            fileStream = new FileStream(fileInfo.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            fileStream.Read(buffer, 0, TextHeaderBytesCount + BinaryHeaderBytesCount);

            // Evaluate text encoding (ascii and ebcdic supported by sgy....for no modern day reason)
            textHeaderEncoding = buffer[0] == 'C' ? Encoding.ASCII : EbcdicEncoding.GetEncoding("EBCDIC-US");
            textHeaders.Add(textHeaderEncoding.GetString(buffer, 0, TextHeaderBytesCount));

            // Endianness and format code
            short bigEFormatCode = bigEBitConverter.ToInt16(buffer, 3224);
            short lilEFormatCode = lilEBitConverter.ToInt16(buffer, 3224);

            if (lilEFormatCode >= 0 || lilEFormatCode <= 8)
            {
                endianBitConverter = new LittleEndianBitConverter();
            }
            if (bigEFormatCode >= 0 || bigEFormatCode <= 8)
            {
                endianBitConverter = new BigEndianBitConverter();
            }
            else
            {
                throw new Exception("Cannot infer endianess from the format code");
            }

            // File Binary Header
            binaryHeader = FileHeader.From(buffer, TextHeaderBytesCount, endianBitConverter);

            // Extended text headers
            fileStream.Seek(3600, SeekOrigin.Begin);
            fileStream.Read(buffer, 0, binaryHeader.ExtendedTextHeadersCount * TextHeaderBytesCount);
            for (int i = 0; i < binaryHeader.ExtendedTextHeadersCount; i++)
            {
                textHeaders.Add(textHeaderEncoding.GetString(buffer, i * TextHeaderBytesCount, TextHeaderBytesCount));
            }

            return(new SgyFile(fileStream, textHeaders, textHeaderEncoding, binaryHeader, endianBitConverter));
        }
Exemple #8
0
 public virtual short vmethod_4()
 {
     byte[] numArray = new byte[2];
     this.stream_0.Read(numArray, 0, 2);
     return(LittleEndianBitConverter.ToInt16(numArray));
 }
Exemple #9
0
        /// <summary>
        /// Read data from stream
        /// </summary>
        /// <param name="s">stream</param>
        /// <returns>data of specified type</returns>
        public static long ToInt64(Stream s)
        {
            byte[] buf = GetBytes(s, sizeof(long));

            return(LittleEndianBitConverter.ToInt16(buf, 0));
        }
Exemple #10
0
        /// <summary>
        /// Read data from stream
        /// </summary>
        /// <param name="s">stream</param>
        /// <returns>data of specified type</returns>
        public static int ToInt32(Stream s)
        {
            byte[] buf = GetBytes(s, sizeof(int));

            return(LittleEndianBitConverter.ToInt16(buf, 0));
        }