Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#

Transform between IEEE, IBM or VAX floating point number formats and bytes expressions

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
16 Dec 2012CPOL1 min read 28.8K   279   12  
This is an alternative for "Transform between IEEE, IBM or VAX floating point number formats and bytes expressions"
  • Converters.zip
    • Converters
      • _ReSharper.Converters
        • AspFileDataCache.dat
        • AssemblyInfoCache.dat
        • BuildScriptCache
          • .crc
          • .version
          • 7
            • 33534609.dat
        • CompletionStatistics
          • Statistics.dat
        • ConditionalNames
          • .crc
          • .version
          • 7
            • 33534609.dat
        • GeneratedFiles
          • .crc
          • .version
          • 7
            • 33534609.dat
        • JavaScriptCache
          • .crc
          • .version
          • 9
            • 3f9c40af.dat
        • JSTest
          • .crc
          • .version
        • ModuleIds.xml
        • MVC
          • .crc
          • .version
        • MvcAnnotations
          • .crc
          • .version
          • 7
            • 33534609.dat
        • NamedArguments
          • .crc
          • .version
          • 7
            • 33534609.dat
        • Pdb
        • PersistentCaches
          • 000005.sst
          • 000008.sst
          • 000011.sst
          • 000012.log
          • CURRENT
          • LOCK
          • LOG
          • LOG.old
          • MANIFEST-000010
        • ProjectFileDataCache
          • ResourceDefaultLanguagePropertyProvider.cache.dat
          • ShouldUseHostCompilerProvider.cache.dat
        • ProjectModel
          • projectModel.dat
        • RecentItems
          • RecentFiles.dat
        • Resources
          • .crc
          • .version
        • SymbolCache.bin
        • TagPrefixes
          • .crc
          • .version
        • WebSymbols
          • .crc
          • .version
        • XmlIndex
          • Accessibility.xml
            • 870D5A60.bin
          • Microsoft.Build.Framework.4.0.0.0.Nullness.Generated.xml
            • E1EA0AC5.bin
          • Microsoft.Build.Framework.xml
            • 3B4F5530.bin
          • Microsoft.Build.Tasks.v4.0.4.0.0.0.Nullness.Generated.xml
            • 50D80BC5.bin
          • Microsoft.Build.Tasks.v4.0.xml
            • FB562A40.bin
          • Microsoft.Build.Utilities.v4.0.4.0.0.0.Nullness.Generated.xml
            • 07BCA185.bin
          • Microsoft.Build.Utilities.v4.0.xml
            • BFE953F8.bin
          • Microsoft.CSharp.4.0.0.0.Nullness.Generated.xml
            • A3901AA5.bin
          • Microsoft.CSharp.xml
            • D5E37BAF.bin
          • mscorlib.4.0.0.0.Contracts.xml
            • 8991B9D5.bin
          • mscorlib.4.0.0.0.Interfaces.Nullness.Generated.xml
            • A19B3A1D.bin
          • mscorlib.4.0.0.0.Nullness.Generated.xml
            • FFF49E25.bin
          • mscorlib.4.0.0.0.Pure.xml
            • 4E4DB2E6.bin
          • mscorlib.I18n.xml
            • 337D6A5E.bin
          • mscorlib.Nullness.Manual.xml
            • AEF920D4.bin
          • mscorlib.Pure.Manual.xml
            • 8F252876.bin
          • mscorlib.ValueFlow.xml
            • 063EC469.bin
          • mscorlib.xml
            • 71BDE13E.bin
            • 743ADB6C.bin
            • CFD54509.bin
          • SMDiagnostics.4.0.0.0.Nullness.Generated.xml
            • 244B92C5.bin
          • System.4.0.0.0.Contracts.xml
            • 9DCFFED5.bin
          • System.4.0.0.0.Nullness.Generated.xml
            • 93108325.bin
          • System.4.0.0.0.Pure.xml
            • 7C4CD7E6.bin
          • System.ComponentModel.DataAnnotations.4.0.0.0.Nullness.Generated.xml
            • 6D21B885.bin
          • System.ComponentModel.DataAnnotations.I18n.xml
            • 51435D7E.bin
          • System.ComponentModel.DataAnnotations.xml
            • C448AF2F.bin
          • System.Configuration.4.0.0.0.Contracts.xml
            • 3D712C95.bin
          • System.Configuration.4.0.0.0.Nullness.Generated.xml
            • A7E8ACE5.bin
          • System.Configuration.4.0.0.0.Pure.xml
            • B18D4E26.bin
          • System.Configuration.Install.4.0.0.0.Contracts.xml
            • 5BC05F35.bin
          • System.Configuration.Install.4.0.0.0.Nullness.Generated.xml
            • F21BA145.bin
          • System.Configuration.Install.xml
            • F42CC37A.bin
          • System.Configuration.xml
            • 092A1070.bin
            • 51C94CD5.bin
          • System.Core.4.0.0.0.Contracts.xml
            • B470AF75.bin
          • System.Core.4.0.0.0.Nullness.Generated.xml
            • 2BE8DB85.bin
          • System.Core.4.0.0.0.Pure.xml
            • 70473D06.bin
          • System.Core.LinqTunnel.xml
            • DC2C01E2.bin
          • System.Core.Nullness.Manual.xml
            • 840981B4.bin
          • System.Core.Pure.Manual.xml
            • E1FED7D6.bin
          • System.Core.xml
            • 11686E5B.bin
            • 9057731E.bin
          • System.Data.4.0.0.0.Contracts.xml
            • E1F5D1F5.bin
          • System.Data.4.0.0.0.Nullness.Generated.xml
            • CB9CCE05.bin
          • System.Data.4.0.0.0.Pure.xml
            • 72ECCF86.bin
          • System.Data.DataSetExtensions.4.0.0.0.Nullness.Generated.xml
            • 9BBF6C05.bin
          • System.Data.DataSetExtensions.I18n.xml
            • 51C970FE.bin
          • System.Data.DataSetExtensions.xml
            • F4BC3A63.bin
          • System.Data.I18n.xml
            • E04152FE.bin
          • System.Data.OracleClient.4.0.0.0.Nullness.Generated.xml
            • 7F324D45.bin
          • System.Data.OracleClient.xml
            • BA96A994.bin
          • System.Data.SqlXml.4.0.0.0.Nullness.Generated.xml
            • 8FCE8945.bin
          • System.Data.SqlXml.xml
            • 48CF6F29.bin
            • 9A03804C.bin
          • System.Data.xml
            • B7FA45F5.bin
          • System.Deployment.4.0.0.0.Nullness.Generated.xml
            • D299FC85.bin
          • System.Deployment.xml
            • 56CC0FFC.bin
          • System.Design.4.0.0.0.Nullness.Generated.xml
            • B79FD0C5.bin
          • System.Design.xml
            • 1169B4F7.bin
          • System.DirectoryServices.4.0.0.0.Nullness.Generated.xml
            • E0BB4C05.bin
          • System.DirectoryServices.Protocols.4.0.0.0.Nullness.Generated.xml
            • 9B439365.bin
          • System.DirectoryServices.Protocols.xml
            • 0A97A989.bin
          • System.DirectoryServices.xml
            • 17DD44C2.bin
          • System.Drawing.4.0.0.0.Contracts.xml
            • 344EECD5.bin
          • System.Drawing.4.0.0.0.Nullness.Generated.xml
            • 2A3B3125.bin
          • System.Drawing.Design.4.0.0.0.Nullness.Generated.xml
            • FBB2BE45.bin
          • System.Drawing.Design.xml
            • 8E3A0B79.bin
          • System.Drawing.Pure.xml
            • 62409462.bin
          • System.Drawing.xml
            • 39C33485.bin
          • System.Dynamic.4.0.0.0.Nullness.Generated.xml
            • 661A5545.bin
          • System.EnterpriseServices.4.0.0.0.Nullness.Generated.xml
            • 5C938D85.bin
          • System.EnterpriseServices.xml
            • BB58925E.bin
          • System.I18n.xml
            • 5B278F5E.bin
          • System.Nullness.Manual.xml
            • A4E225D4.bin
          • System.Numerics.4.0.0.0.Nullness.Generated.xml
            • 0917EF05.bin
          • System.Numerics.xml
            • B6F071FD.bin
            • F65D6778.bin
          • System.Runtime.Caching.4.0.0.0.Nullness.Generated.xml
            • ACCA4445.bin
          • System.Runtime.Caching.xml
            • 129CA5BE.bin
          • System.Runtime.Remoting.4.0.0.0.Nullness.Generated.xml
            • 9F932845.bin
          • System.Runtime.Remoting.xml
            • A5303EB6.bin
          • System.Runtime.Serialization.4.0.0.0.Nullness.Generated.xml
            • 99073725.bin
          • System.Runtime.Serialization.Attributes.xml
            • AA29277F.bin
          • System.Runtime.Serialization.Formatters.Soap.2.0.0.0.Contracts.xml
            • 0AB585D3.bin
          • System.Runtime.Serialization.Formatters.Soap.4.0.0.0.Contracts.xml
            • 13934755.bin
          • System.Runtime.Serialization.Formatters.Soap.4.0.0.0.Nullness.Generated.xml
            • 324D5BA5.bin
          • System.Runtime.Serialization.Formatters.Soap.xml
            • E636A127.bin
          • System.Runtime.Serialization.xml
            • 7CDC3839.bin
          • System.Security.4.0.0.0.Contracts.xml
            • 642F8175.bin
          • System.Security.4.0.0.0.Nullness.Generated.xml
            • 1C73ED85.bin
          • System.Security.4.0.0.0.Pure.xml
            • 66FACF06.bin
          • System.Security.xml
            • 55841E76.bin
            • A33E1173.bin
          • System.ServiceProcess.4.0.0.0.Nullness.Generated.xml
            • 01494905.bin
          • System.ServiceProcess.xml
            • DEE59913.bin
          • System.Transactions.4.0.0.0.Nullness.Generated.xml
            • C6F46385.bin
          • System.Transactions.xml
            • DBFAB5F2.bin
          • System.Web.4.0.0.0.Contracts.xml
            • 96C4E455.bin
          • System.Web.4.0.0.0.Nullness.Generated.xml
            • 579F98A5.bin
          • System.Web.4.0.0.0.Pure.xml
            • A1BC4D66.bin
          • System.Web.ApplicationServices.4.0.0.0.Nullness.Generated.xml
            • B6473DA5.bin
          • System.Web.ApplicationServices.xml
            • D971064F.bin
          • System.Web.Attributes.xml
            • 032E88FF.bin
          • System.Web.I18n.xml
            • A93984DE.bin
          • System.Web.RegularExpressions.xml
            • 00CA1780.bin
          • System.Web.Services.4.0.0.0.Nullness.Generated.xml
            • 3E344B85.bin
          • System.Web.Services.Attributes.xml
            • D91AB31F.bin
          • System.Web.Services.xml
            • 70FE39D5.bin
          • System.Web.xml
            • 7F630C15.bin
          • System.Windows.Forms.4.0.0.0.Contracts.xml
            • 5B44F5D5.bin
          • System.Windows.Forms.4.0.0.0.Nullness.Generated.xml
            • E50B5A25.bin
          • System.Windows.Forms.4.0.0.0.Pure.xml
            • F80B6EE6.bin
          • System.Windows.Forms.I18n.xml
            • 493F265E.bin
          • System.Windows.Forms.Manual.xml
            • 2DC6F1AA.bin
          • System.Windows.Forms.xml
            • EF816551.bin
          • System.Xaml.4.0.0.0.Nullness.Generated.xml
            • F1742545.bin
          • System.Xaml.xml
            • C856BB1D.bin
          • System.Xml.4.0.0.0.Contracts.xml
            • BD8E5935.bin
          • System.Xml.4.0.0.0.Nullness.Generated.xml
            • FC42DB45.bin
          • System.Xml.4.0.0.0.Pure.xml
            • 1C612E46.bin
          • System.Xml.Attributes.xml
            • 6BFCF35F.bin
          • System.Xml.I18n.xml
            • 16989EBE.bin
          • System.Xml.Linq.4.0.0.0.Contracts.xml
            • 4B08AC75.bin
          • System.Xml.Linq.4.0.0.0.Nullness.Generated.xml
            • 8DCD7885.bin
          • System.Xml.Linq.4.0.0.0.Pure.xml
            • 9BAD1A06.bin
          • System.Xml.Linq.I18n.xml
            • 8C9F1D7E.bin
          • System.Xml.Linq.xml
            • 21CA9248.bin
          • System.Xml.xml
            • 04E9321C.bin
            • 48B5EA79.bin
          • System.xml
            • 083CE63E.bin
            • 82B05E4B.bin
            • C3B3EEEE.bin
      • bin
        • Debug
          • Converters.dll
          • Converters.pdb
        • Release
      • Byte2SEF.cs
      • Common.cs
      • Converter.cs
      • Converters.csproj
      • Converters.sln
      • Converters.v11.suo
      • Enums.cs
      • Float2SEF.cs
      • IbmFloat.cs
      • IeeeFloat.cs
      • libNumber.cs
      • obj
      • Properties
      • SEF2Byte.cs
      • SEF2Float.cs
      • Test
      • VaxFloat.cs
  • converters-noexe.zip
  • converters.zip
  • Converters-noexe.zip
namespace Savian.CoreConverters.Converters
{
    internal static class GlobalMembersSEF2Byte
    {
        /***************************************************************************
                                  SEF2Byte.c  -  description
                                     -------------------
            begin                : November 2005
            copyright            : (C) 2005 by John Jiyang Hou
            email                : jyhou69@hotmail.com
         ***************************************************************************/

        /***************************************************************************
         *                                                                         *
         *   This program is free software; you can redistribute it and/or modify  *
         *   it under the terms of the GNU General Public License as published by  *
         *   the Free Software Foundation; either version 2 of the License, or     *
         *   (at your option) any later version.                                   *
         *                                                                         *
         ***************************************************************************/


        internal static int SingleSEF2Byte(FloatType FloatType, byte S, int E, uint F, byte[] bytes)
        {

            switch (FloatType)
            {
                case FloatType.IeeeSingleFloat:
                    if (Common.Endian == Endian.BigEndian)
                    {
                        bytes[0] = (byte) (S + ((E & 0xfe) >> 1));
                        bytes[1] = (byte) (((E & 0x01) << 7) + ((F & 0x007f0000) >> 16));
                        bytes[2] = (byte) ((F & 0x0000ff00) >> 8);
                        bytes[3] = (byte) (F & 0x000000ff);
                    }
                    else // LittleEndian
                    {
                        bytes[3] = (byte) (S + ((E & 0xfe) >> 1));
                        bytes[2] = (byte) (((E & 0x01) << 7) + ((F & 0x007f0000) >> 16));
                        bytes[1] = (byte) ((F & 0x0000ff00) >> 8);
                        bytes[0] = (byte) (F & 0x000000ff);
                    }

                    break;

                case FloatType.IbmSingleFloat:
                    if (Common.Endian == Endian.BigEndian)
                    {
                        bytes[0] = (byte) (S + E);
                        bytes[1] = (byte) ((F & 0x00ff0000) >> 16);
                        bytes[2] = (byte) ((F & 0x0000ff00) >> 8);
                        bytes[3] = (byte) (F & 0x000000ff);
                    }
                    else // LittleEndian
                    {
                        bytes[3] = (byte) (S + E);
                        bytes[2] = (byte) ((F & 0x00ff0000) >> 16);
                        bytes[1] = (byte) ((F & 0x0000ff00) >> 8);
                        bytes[0] = (byte) (F & 0x000000ff);
                    }

                    break;
                case FloatType.VaxSingleFloat:
                    bytes[1] = (byte) (S + ((E & 0xfe) >> 1));
                    bytes[0] = (byte) (((E & 0x01) << 7) + ((F & 0x007f0000) >> 16));
                    bytes[3] = (byte) ((F & 0x0000ff00) >> 8);
                    bytes[2] = (byte) (F & 0x000000ff);

                    break;
            }

            return 0;
        }

        internal static int DoubleSEF2Byte(FloatType FloatType, byte S, int E, uint L1, uint L2, byte[] bytes)
        {

            switch (FloatType)
            {
                case FloatType.IeeeDoubleFloat:
                    if (Common.Endian == Endian.BigEndian)
                    {
                        bytes[0] = (byte) (S + ((E & 0x000007f0) >> 4));
                        bytes[1] = (byte) (((E & 0x0000000f) << 4) + ((L1 & 0x000f0000) >> 16));
                        bytes[2] = (byte) ((L1 & 0x0000ff00) >> 8);
                        bytes[3] = (byte) (L1 & 0x000000ff);
                        bytes[4] = (byte) ((L2 & 0xff000000) >> 24);
                        bytes[5] = (byte) ((L2 & 0x00ff0000) >> 16);
                        bytes[6] = (byte) ((L2 & 0x0000ff00) >> 8);
                        bytes[7] = (byte) ((L2 & 0x000000ff));
                    }
                    else
                    {
                        bytes[7] = (byte) (S + ((E & 0x000007f0) >> 4));
                        bytes[6] = (byte) (((E & 0x0000000f) << 4) + ((L1 & 0x000f0000) >> 16));
                        bytes[5] = (byte) ((L1 & 0x0000ff00) >> 8);
                        bytes[4] = (byte) (L1 & 0x000000ff);
                        bytes[3] = (byte) ((L2 & 0xff000000) >> 24);
                        bytes[2] = (byte) ((L2 & 0x00ff0000) >> 16);
                        bytes[1] = (byte) ((L2 & 0x0000ff00) >> 8);
                        bytes[0] = (byte) ((L2 & 0x000000ff));
                    }

                    break;
                case FloatType.IbmDoubleFloat:
                    if (Common.Endian == Endian.BigEndian)
                    {
                        bytes[0] = (byte) (S + E);
                        bytes[1] = (byte) ((L1 & 0x00ff0000) >> 16);
                        bytes[2] = (byte) ((L1 & 0x0000ff00) >> 8);
                        bytes[3] = (byte) (L1 & 0x000000ff);
                        bytes[4] = (byte) ((L2 & 0xff000000) >> 24);
                        bytes[5] = (byte) ((L2 & 0x00ff0000) >> 16);
                        bytes[6] = (byte) ((L2 & 0x0000ff00) >> 8);
                        bytes[7] = (byte) ((L2 & 0x000000ff));
                    }
                    else // LittleEndian
                    {
                        bytes[7] = (byte) (S + E);
                        bytes[6] = (byte) ((L1 & 0x00ff0000) >> 16);
                        bytes[5] = (byte) ((L1 & 0x0000ff00) >> 8);
                        bytes[4] = (byte) (L1 & 0x000000ff);
                        bytes[3] = (byte) ((L2 & 0xff000000) >> 24);
                        bytes[2] = (byte) ((L2 & 0x00ff0000) >> 16);
                        bytes[1] = (byte) ((L2 & 0x0000ff00) >> 8);
                        bytes[0] = (byte) ((L2 & 0x000000ff));
                    }

                    break;
            }

            return 0;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Savian
United States United States
SAS, Microsoft developer/consultant.

Comments and Discussions