Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3931146
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:19:27+00:00 2026-05-19T23:19:27+00:00

i am trying to convert double to pascal real but when i convert 0.23

  • 0

i am trying to convert double to pascal real but when i convert 0.23 to real i got 0.23999999 real how can i truncate all 9999 to 0000.

public static byte[] Double2Real48(double d)
{
    byte[] r48 = new byte[6];
    byte[] da = BitConverter.GetBytes(d);

    for (int i = 0; i < r48.Length; i++)
        r48[i] = 0;

    //Copy the negative flag
    r48[5] |= (byte)(da[7] & 0x80);

    //Get the expoent
    byte b1 = (byte)(da[7] & 0x7f);
    ushort n = (ushort)(b1 << 4);
    byte b2 = (byte)(da[6] & 0xf0);
    b2 >>= 4;
    n |= b2;

    if (n == 0)
        return r48;

    byte ex = (byte)(n - 1023);
    r48[0] = (byte)(ex + 129);

    //Copy the Mantissa
    r48[5] |= (byte)((da[6] & 0x0f) << 3);//Get the last four bits
    r48[5] |= (byte)((da[5] & 0xe0) >> 5);//Get the first three bits

    r48[4] = (byte)((da[5] & 0x1f) << 3);//Get the last 5 bits
    r48[4] |= (byte)((da[4] & 0xe0) >> 5);//Get the first three bits

    r48[3] = (byte)((da[4] & 0x1f) << 3);//Get the last 5 bits
    r48[3] |= (byte)((da[3] & 0xe0) >> 5);//Get the first three bits

    r48[2] = (byte)((da[3] & 0x1f) << 3);//Get the last 5 bits
    r48[2] |= (byte)((da[2] & 0xe0) >> 5);//Get the first three bits

    r48[1] = (byte)((da[2] & 0x1f) << 3);//Get the last 5 bits
    r48[1] |= (byte)((da[1] & 0xe0) >> 5);//Get the first three bits

    return r48;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:19:28+00:00Added an answer on May 19, 2026 at 11:19 pm

    Here are my conversion routines. Note that I haven’t put anything special here for treating IEEE infinities or NaN values.

    static byte[] DoubleToReal48(double d)
    {
        byte[] r = new byte[6];
    
        long bits = BitConverter.DoubleToInt64Bits(d);
        bool negative = ((bits >> 63) & 1) != 0;
        long exponent = ((bits >> 52) & 0x7FF) - 1023;
        long mantissa = bits & 0xFFFFFFFFFFFFFL;
    
        long raw = (negative ? 1 : 0);
        raw = (raw << 39) | (mantissa >> 13);
        raw = (raw << 8) | ((exponent + 129) & 0xFF);
    
        for (int k = 0; k < 6; k++)
        {
            r[k] = (byte)(raw & 0xFF);
            raw >>= 8;
        }
        return r;
    }
    
    static double Real48ToDouble(byte[] r)
    {
        long raw = 0;
        for (int k = 5; k >= 0; k--)
        {
            raw = (raw << 8) | r[k];
        }
    
        long mantissa = (raw << 5) & 0xFFFFFFFFFD000L;
        long exponent = (((raw & 0xFF) - 129 + 1023) & 0x7FF) << 52;
        long sign = (((raw & ~0x7FFFFFFFFFFFL) != 0) ? 1 : 0) << 63;
    
        return BitConverter.Int64BitsToDouble(sign | exponent | mantissa);
    }
    

    There is some loss of precision with roundtrip conversions, but the results are basically correct. [Real48ToDouble(DoubleToReal48(0.23)) returns 0.229999999999563]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert some code that worked great in VB, but I can't
I'm trying to convert a string to a double value but it's not returning
I am trying to convert a double to a string in a native NT
I am trying to convert scientific double to decimal double in java. I am
I am having some issues trying to convert a double to C++ string. Here
I'm trying to convert std::string to float/double . I tried: std::string num = 0.6;
I am trying to convert a string to a double. The code is very
I'm trying to convert a string to a double value in .Net 3.5. Quite
I am trying to convert a double to a string on the stack from
I've been trying to convert SVG images to PNG using C#, without having to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.