Esempio n. 1
0
        protected override int MaxStringifiedElementLength => 4; //1 length + 3 data

        public override byte[] GetByteValue(MemoryMappedViewAccessor source, bool wholeArray)
        {
            byte[] value = base.GetByteValue(source, wholeArray);

            if (Stringify)
            {
                int elemsToRead = wholeArray ? MaxArrayLength : ArrayLength;

                for (int i = 0; i < elemsToRead; i++)
                {
                    string stringified = value[i].ToString(CultureInfo.InvariantCulture);
                    byte[] buffer      = new byte[stringified.Length + 1];
                    buffer[0] = (byte)stringified.Length;
                    ASCII.GetBytes(stringified, 0, stringified.Length, buffer, 1);

                    StringifyBuffer.Append(buffer);
                }

                return(StringifyBuffer.Flush());
            }
            else
            {
                return(value);
            }
        }
Esempio n. 2
0
        public override byte[] GetByteValue(MemoryMappedViewAccessor source, bool wholeArray = false)
        {
            byte[] value = GetConvertedValue(source, wholeArray);

            if (Stringify)
            {
                int elemsToRead = wholeArray ? MaxArrayLength : ArrayLength;

                for (int i = 0; i < elemsToRead; i++)
                {
                    byte[] buffer;
                    if (_CastToInt == CastMode.NONE)
                    {
                        string stringified = ToSingle(value, i * ElementSize).ToString(StringifyFormat, CultureInfo.InvariantCulture);
                        if (_DecimalLength > 0)
                        {
                            if (stringified.IndexOf('.') != -1)
                            {
                                string[] parts = stringified.Split('.');
                                if (parts[0].Length > 11 || parts[1].Length > 8)
                                {
                                    buffer     = ASCII.GetBytes(new string('#', 21));
                                    buffer[0]  = 11;
                                    buffer[12] = 8;
                                }
                                else
                                {
                                    buffer = new byte[parts[0].Length + parts[1].Length + 2];

                                    buffer[0] = (byte)parts[0].Length;
                                    ASCII.GetBytes(parts[0], 0, parts[0].Length, buffer, 1);

                                    buffer[parts[0].Length + 1] = (byte)parts[1].Length;
                                    ASCII.GetBytes(parts[1], 0, parts[1].Length, buffer, parts[0].Length + 2);
                                }
                            }
                            else
                            {
                                if (stringified.Length > 11)
                                {
                                    buffer = ASCII.GetBytes(new string('#', 13));

                                    buffer[0]  = 11;
                                    buffer[12] = 0;
                                }
                                else
                                {
                                    buffer = new byte[stringified.Length + 2];

                                    buffer[0] = (byte)stringified.Length;
                                    buffer[stringified.Length + 1] = 0;
                                    ASCII.GetBytes(stringified, 0, stringified.Length, buffer, 1);
                                }
                            }
                        }
                        else
                        {
                            if (stringified.Length > 11)
                            {
                                buffer = ASCII.GetBytes(new string('#', 12));

                                buffer[0] = 11;
                            }
                            else
                            {
                                buffer = new byte[stringified.Length + 1];

                                buffer[0] = (byte)stringified.Length;
                                ASCII.GetBytes(stringified, 0, stringified.Length, buffer, 1);
                            }
                        }
                    }
                    else
                    {
                        string stringified = ToInt32(value, i * ElementSize).ToString(CultureInfo.InvariantCulture);
                        buffer    = new byte[stringified.Length + 1];
                        buffer[0] = (byte)stringified.Length;
                        ASCII.GetBytes(stringified, 0, stringified.Length, buffer, 1);
                    }

                    StringifyBuffer.Append(buffer);
                }

                return(StringifyBuffer.Flush());
            }
            else
            {
                return(value);
            }
        }