/// <summary> /// Treats the internal data as an integer. /// </summary> /// <param name="value"></param> public void SetData(float value) { internalDataType = DatumDataType.Float; isDirty = true; data = BitConverter.GetBytes(value); // Do we need to endian swap? if (BitConverter.IsLittleEndian) { Array.Reverse(data); } }
/// <summary> /// Sets the data from a string. /// </summary> /// <param name="values"></param> public void SetData(string text) { internalDataType = DatumDataType.String; datumLengthInBits = text.Length * 8; if (string.IsNullOrEmpty(text)) { data = null; } else { // Add padding directly to the string. text = text.PadRight(text.Length + (8 - (text.Length % 8))); data = Encoding.ASCII.GetBytes(text); } }
/// <summary> /// Treats the internal data as a list of doubles. /// </summary> /// <returns>List of converted doubles or null if no data exists or something went wrong.</returns> public double[] GetAsDoubleList() { internalDataType = DatumDataType.Doubles; if (data == null) { return(null); } if (datumLengthInBits % 64 != 0) { Debug.LogWarning("The data is not the correct size (must be multiple of 64 bits)." + "This data is " + datumLengthInBits + " bits. The last " + datumLengthInBits % 64 + " bits will be ignored."); } // Validate that we actually have the data if (data.Length != DatumLengthIncludingPadding / 8) { Debug.LogError("Data length is incorrect, it should be " + DatumLengthIncludingPadding + " but it is " + data.Length * 8); return(null); } int valuesToConvert = Mathf.FloorToInt(datumLengthInBits / 64.0f); double[] values = new double[valuesToConvert]; byte[] tmpBuffer = new byte[8]; for (int i = 0; i < valuesToConvert; ++i) { // Do we need to endian swap? if (BitConverter.IsLittleEndian) { Buffer.BlockCopy(data, i * 8, tmpBuffer, 0, 8); Array.Reverse(tmpBuffer); values[i] = BitConverter.ToDouble(tmpBuffer, 0); } else { values[i] = BitConverter.ToDouble(data, i * 8); } } return(values); }
/// <summary> /// Treats the internal data as a string. /// </summary> /// <returns>Converted string.</returns> public string GetAsString() { internalDataType = DatumDataType.String; if (data == null) { return(string.Empty); } if (datumLengthInBits % 8 != 0) { Debug.LogWarning("The data is not the correct size (must be multiple of 8 bits)." + "This data is " + datumLengthInBits + " bits. The last " + datumLengthInBits % 8 + " bits will be ignored."); } return(Encoding.ASCII.GetString(data, 0, Mathf.FloorToInt(datumLengthInBits / 8.0f))); }
/// <summary> /// Treats the internal data as a float. /// </summary> /// <param name="value"></param> public float GetAsFloat() { if (data == null) { data = new byte[4]; } internalDataType = DatumDataType.Float; // Do we need to endian swap? if (BitConverter.IsLittleEndian) { byte[] dataCpy = new byte[4]; Array.Copy(data, dataCpy, 4); Array.Reverse(dataCpy); return(BitConverter.ToSingle(dataCpy, 0)); } else { return(BitConverter.ToSingle(data, 0)); } }