BerLib.BerEncoding.EncodeReal C# (CSharp) Method

EncodeReal() public static method

public static EncodeReal ( IBerOutput output, double value ) : int
output IBerOutput
value double
return int
        public static int EncodeReal(IBerOutput output, double value)
        {
            var size = 0;

             if(double.IsPositiveInfinity(value))
             {
            output.WriteByte(0x40); // 01000000 Value is PLUS-INFINITY
            size = 1;
             }
             else if(double.IsNegativeInfinity(value)) // negative infinity
             {
            output.WriteByte(0x41); // 01000001 Value is MINUS-INFINITY
            size = 1;
             }
             else if (double.IsNaN(value))
             {
            output.WriteByte(0x42); // 01000010 Value is NOT-A-NUMBER
            size = 1;
             }
             else
             {
            long longValue = DoubleToInt64Bits(value);

            if (longValue != 0)
            {
               long exponent = ((0x7FF0000000000000L & longValue) >> 52) - 1023;
               long mantissa = 0x000FFFFFFFFFFFFFL & longValue;
               mantissa |= 0x0010000000000000L; // set virtual delimeter

               // normalize mantissa (required by CER and DER)
               while ((mantissa & 0xFF) == 0)
                  mantissa >>= 8;

               while ((mantissa & 0x01) == 0)
                  mantissa >>= 1;

               int exponentLength = GetLongLength(exponent);
               Debug.Assert(exponentLength <= 3);

               byte preamble = 0x80;
               preamble |= (byte)(exponentLength - 1);

               if (((ulong)longValue & 0x8000000000000000UL) != 0)
                  preamble |= 0x40; // Sign

               output.WriteByte(preamble);

               size++;
               size += EncodeLong(output, exponent, exponentLength); // signed exponent
               size += EncodeLong(output, mantissa, GetLongLength(mantissa, false)); // unsigned mantissa
            }
             }

             return size;
        }

Usage Example

Example #1
0
        public static byte[] Encode(string format, params object[] args)
        {
            var output     = new BerMemoryOutput();
            var paramIndex = 0;

            for (int charIndex = 0; charIndex < format.Length; charIndex++)
            {
                var ch = format[charIndex];

                switch (ch)
                {
                case '{':
                    EncodeHeader(output, TypeTags.Sequence, BerDefinitions.IndefiniteLength);
                    break;

                case '}':
                    output.WriteByte(0);
                    output.WriteByte(0);
                    break;

                case 'b':
                {
                    EncodeHeader(output, TypeTags.Boolean, 1);
                    BerEncoding.EncodeBoolean(output, (bool)args[paramIndex++]);
                    break;
                }

                case 'i':
                case 'd':
                {
                    var value       = (int)args[paramIndex++];
                    var valueLength = BerEncoding.GetIntegerLength(value);

                    EncodeHeader(output, TypeTags.Integer, valueLength);
                    BerEncoding.EncodeInteger(output, value, (int)valueLength);
                    break;
                }

                case 'l':
                case 't':
                {
                    var value       = (long)args[paramIndex++];
                    var valueLength = BerEncoding.GetLongLength(value);

                    EncodeHeader(output, TypeTags.Integer, valueLength);
                    BerEncoding.EncodeLong(output, value, (int)valueLength);
                    break;
                }

                case 's':
                {
                    var value       = (string)args[paramIndex++];
                    var valueLength = BerEncoding.GetUtf8StringLength(value);

                    EncodeHeader(output, TypeTags.UTF8String, valueLength);
                    BerEncoding.EncodeUtf8String(output, value);
                    break;
                }

                case 'y':
                {
                    var value       = (byte[])args[paramIndex++];
                    var valueLength = value.Length;

                    EncodeHeader(output, TypeTags.OctetString, valueLength);
                    BerEncoding.EncodeByteArray(output, value);
                    break;
                }

                case 'f':
                case 'r':
                {
                    var value       = (double)args[paramIndex++];
                    var local       = new BerMemoryOutput();
                    var valueLength = BerEncoding.EncodeReal(local, value);
                    EncodeHeader(output, TypeTags.Real, valueLength);
                    output.WriteBytes(local.Memory);
                    break;
                }

                case 'o':
                {
                    var value       = (int[])args[paramIndex++];
                    var local       = new BerMemoryOutput();
                    var valueLength = BerEncoding.EncodeRelativeOid(local, value);
                    EncodeHeader(output, TypeTags.RelativeOid, valueLength);
                    output.WriteBytes(local.Memory);
                    break;
                }

                case 'g':
                {
                    var value       = (DateTime)args[paramIndex++];
                    var local       = new BerMemoryOutput();
                    var valueLength = BerEncoding.EncodeGeneralizedTime(local, value);
                    EncodeHeader(output, TypeTags.GeneralizedTime, valueLength);
                    output.WriteBytes(local.Memory);
                    break;
                }
                }
            }

            return(output.ToArray());
        }