ToUInt64() public static method

Returns a 64-bit unsigned integer converted from two bytes at a specified position in a byte array.
public static ToUInt64 ( byte array, int index ) : ulong
array byte Specify an array of bytes.
index int Specify the starting position.
return ulong
Ejemplo n.º 1
0
        /// <summary>
        /// Used to de-serialize the element.
        /// </summary>
        /// <param name="byteArray">A Byte array</param>
        /// <param name="currentIndex">Start position</param>
        /// <param name="lengthOfItems">The length of the items</param>
        protected override void DeserializeItemsFromByteArray(byte[] byteArray, ref int currentIndex, int lengthOfItems)
        {
            if (lengthOfItems != 8)
            {
                throw new StreamObjectParseErrorException(currentIndex, "DataSize", "Stream Object over-parse error", null);
            }

            this.DataSize = LittleEndianBitConverter.ToUInt64(byteArray, currentIndex);
            currentIndex += 8;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Read specified bit length content as an UInt64 type and increase the bit offset.
 /// </summary>
 /// <param name="readingLength">Specify the reading bit length.</param>
 /// <returns>Return the UInt64 type value.</returns>
 public ulong ReadUInt64(int readingLength)
 {
     byte[] uint64Bytes = this.GetBytes(readingLength, 8);
     return(LittleEndianBitConverter.ToUInt64(uint64Bytes, 0));
 }
        /// <summary>
        /// Deserialize response from byte array.
        /// </summary>
        /// <param name="byteArray">Server returned message.</param>
        /// <param name="startIndex">The index special where start.</param>
        /// <returns>The instance of CellResponse.</returns>
        public static FsshttpbResponse DeserializeResponseFromByteArray(byte[] byteArray, int startIndex)
        {
            int index = startIndex;

            FsshttpbResponse response = new FsshttpbResponse();

            response.ProtocolVersion = LittleEndianBitConverter.ToUInt16(byteArray, index);
            index += 2;

            response.MinimumVersion = LittleEndianBitConverter.ToUInt16(byteArray, index);
            index += 2;

            response.Signature = LittleEndianBitConverter.ToUInt64(byteArray, index);
            index += 8;

            int length = 0;
            StreamObjectHeaderStart streamObjectHeader;

            if ((length = StreamObjectHeaderStart.TryParse(byteArray, index, out streamObjectHeader)) == 0)
            {
                throw new ResponseParseErrorException(index, "Failed to parse the response header", null);
            }

            if (!(streamObjectHeader is StreamObjectHeaderStart32bit))
            {
                throw new ResponseParseErrorException(index, "Unexpected 16-bit response stream object header, expect 32-bit stream object header for Response", null);
            }

            if (streamObjectHeader.Type != StreamObjectTypeHeaderStart.FsshttpbResponse)
            {
                throw new ResponseParseErrorException(index, "Failed to extract the response header type, unexpected value " + streamObjectHeader.Type, null);
            }

            if (streamObjectHeader.Length != 1)
            {
                throw new ResponseParseErrorException(index, "Response object over-parse error", null);
            }

            index += length;
            response.ResponseStart = streamObjectHeader as StreamObjectHeaderStart32bit;

            response.Status   = (byteArray[index] & 0x1) == 0x1 ? true : false;
            response.Reserved = (byte)(byteArray[index] >> 1);
            index            += 1;

            try
            {
                if (response.Status)
                {
                    response.ResponseError      = StreamObject.GetCurrent <ResponseError>(byteArray, ref index);
                    response.DataElementPackage = null;
                    response.CellSubResponses   = null;
                }
                else
                {
                    DataElementPackage package;
                    if (StreamObject.TryGetCurrent <DataElementPackage>(byteArray, ref index, out package))
                    {
                        response.DataElementPackage = package;
                    }

                    response.CellSubResponses = new List <FsshttpbSubResponse>();
                    FsshttpbSubResponse subResponse;
                    while (StreamObject.TryGetCurrent <FsshttpbSubResponse>(byteArray, ref index, out subResponse))
                    {
                        response.CellSubResponses.Add(subResponse);
                    }
                }

                response.ResponseEnd = BasicObject.Parse <StreamObjectHeaderEnd16bit>(byteArray, ref index);
            }
            catch (StreamObjectParseErrorException streamObjectException)
            {
                throw new ResponseParseErrorException(index, streamObjectException);
            }
            catch (DataElementParseErrorException dataElementException)
            {
                throw new ResponseParseErrorException(index, dataElementException);
            }
            catch (KnowledgeParseErrorException knowledgeException)
            {
                throw new ResponseParseErrorException(index, knowledgeException);
            }

            if (index != byteArray.Length)
            {
                throw new ResponseParseErrorException(index, "Failed to pass the whole response, not reach the end of the byte array", null);
            }

            return(response);
        }