/// <summary> /// Reads a integer value from the buffer. /// </summary> private int ReadField(TsCCpxContext context, Integer field, out object fieldValue) { fieldValue = null; byte[] buffer = context.Buffer; // initialize serialization paramters. int length = (field.LengthSpecified) ? (int)field.Length : 4; bool signed = field.Signed; // apply defaults for built in types. if (field.GetType() == typeof(Int8)) { length = 1; signed = true; } else if (field.GetType() == typeof(Int16)) { length = 2; signed = true; } else if (field.GetType() == typeof(Int32)) { length = 4; signed = true; } else if (field.GetType() == typeof(Int64)) { length = 8; signed = true; } else if (field.GetType() == typeof(UInt8)) { length = 1; signed = false; } else if (field.GetType() == typeof(UInt16)) { length = 2; signed = false; } else if (field.GetType() == typeof(UInt32)) { length = 4; signed = false; } else if (field.GetType() == typeof(UInt64)) { length = 8; signed = false; } // check if there is enough data left. if (buffer.Length - context.Index < length) { throw new TsCCpxInvalidDataInBufferException("Unexpected end of buffer."); } // copy and swap bytes if required. byte[] bytes = new byte[length]; for (int ii = 0; ii < length; ii++) { bytes[ii] = buffer[context.Index + ii]; } if (context.BigEndian) { SwapBytes(bytes, 0, length); } // convert to object. if (signed) { switch (length) { case 1: { if (bytes[0] < 128) { fieldValue = (sbyte)bytes[0]; } else { fieldValue = (sbyte)(0 - bytes[0]); } break; } case 2: { fieldValue = BitConverter.ToInt16(bytes, 0); break; } case 4: { fieldValue = BitConverter.ToInt32(bytes, 0); break; } case 8: { fieldValue = BitConverter.ToInt64(bytes, 0); break; } default: { fieldValue = bytes; break; } } } else { switch (length) { case 1: { fieldValue = bytes[0]; break; } case 2: { fieldValue = BitConverter.ToUInt16(bytes, 0); break; } case 4: { fieldValue = BitConverter.ToUInt32(bytes, 0); break; } case 8: { fieldValue = BitConverter.ToUInt64(bytes, 0); break; } default: { fieldValue = bytes; break; } } } return(length); }
/// <summary> /// Writes a integer value from to the buffer. /// </summary> private int WriteField(TsCCpxContext context, Integer field, object fieldValue) { byte[] buffer = context.Buffer; // initialize serialization paramters. int length = (field.LengthSpecified) ? (int)field.Length : 4; bool signed = field.Signed; // apply defaults for built in types. if (field.GetType() == typeof(Int8)) { length = 1; signed = true; } else if (field.GetType() == typeof(Int16)) { length = 2; signed = true; } else if (field.GetType() == typeof(Int32)) { length = 4; signed = true; } else if (field.GetType() == typeof(Int64)) { length = 8; signed = true; } else if (field.GetType() == typeof(UInt8)) { length = 1; signed = false; } else if (field.GetType() == typeof(UInt16)) { length = 2; signed = false; } else if (field.GetType() == typeof(UInt32)) { length = 4; signed = false; } else if (field.GetType() == typeof(UInt64)) { length = 8; signed = false; } // only write to the buffer if it has been allocated. if (buffer != null) { // check if there is enough data left. if (buffer.Length - context.Index < length) { throw new TsCCpxInvalidDataToWriteException("Unexpected end of buffer."); } // copy and swap bytes if required. byte[] bytes = null; if (signed) { switch (length) { case 1: { bytes = new byte[1]; sbyte value = Convert.ToSByte(fieldValue); if (value < 0) { bytes[0] = (byte)(Byte.MaxValue + value + 1); } else { bytes[0] = (byte)value; } break; } case 2: { bytes = BitConverter.GetBytes(Convert.ToInt16(fieldValue)); break; } case 4: { bytes = BitConverter.GetBytes(Convert.ToInt32(fieldValue)); break; } case 8: { bytes = BitConverter.GetBytes(Convert.ToInt64(fieldValue)); break; } default: { bytes = (byte[])fieldValue; break; } } } else { switch (length) { case 1: { bytes = new byte[] { Convert.ToByte(fieldValue) }; break; } case 2: { bytes = BitConverter.GetBytes(Convert.ToUInt16(fieldValue)); break; } case 4: { bytes = BitConverter.GetBytes(Convert.ToUInt32(fieldValue)); break; } case 8: { bytes = BitConverter.GetBytes(Convert.ToUInt64(fieldValue)); break; } default: { bytes = (byte[])fieldValue; break; } } } // copy and swap bytes. if (context.BigEndian) { SwapBytes(bytes, 0, length); } // write bytes to buffer. for (int ii = 0; ii < bytes.Length; ii++) { buffer[context.Index + ii] = bytes[ii]; } } return(length); }