CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2008
    Posts
    10

    [RESOLVED] Double to Binary Array for IO output

    Hey guys, please help me with these functions.
    I have this presently working for my Long Int Variables (4 bytes/char's). But I cant for the life of me get this working for Doubles (8 bytes).
    I am still rather new to C++.

    My Functions:

    void DblToBin(long double iVal, unsigned char * vPtr)
    {
    long i = 0;
    unsigned char pChars[8];
    memcpy( &pChars[0] ,&iVal, 8);

    vPtr=pChars;
    return;
    }

    long double BinToDbl( unsigned char * vPtr)
    {
    long double iVal = 0;
    memcpy( &iVal ,&vPtr[0], 8);
    return iVal;
    }

    The issue:
    If I pass a Double Variable in DblToBin and back through BinToDbl the result is always 0, obviously I need this to result as the same as I input.

    The Goal:
    I am developing a high performance OLAP database and to optimise my load times I am using a fixed length binary File format.

    Any help is greatly appreciated.

    I am using Bloodshed Dev C++ under Windows XP.

  2. #2
    Join Date
    May 2002
    Posts
    1,435

    Re: Double to Binary Array for IO output

    In DblToBin() you are copying the double to a local array and then assigning the address of that array to a LOCAL function parameter. None of what you do leaves the function. I would recommend copying the memory directly from iVal to vPtr.

    BinToDbl() seems to be OK.

  3. #3
    Join Date
    Jul 2007
    Location
    Richmond, BC
    Posts
    79

    Re: Double to Binary Array for IO output

    Your function DblToBin cannot work as you expect becouse
    you are trying to return a pointer to the local variable (vPtr=pChars).
    My suggestion is to change it to
    Code:
    unsigned char* DblToBin(long double iVal)
    {
      long i = 0;
      unsigned char *pChars = new unsigned char[8];
      memcpy(pChars  ,&iVal, 8);
      return pChars;
    }

  4. #4
    Join Date
    Jun 2008
    Posts
    10

    Re: Double to Binary Array for IO output

    Thanks for that, I don't know how I missed it.
    my problem remains though, its still converting everything to 0 when double parsed.

    I need to get it so it produces the same output as the input when called like so:

    Long Double iVal = 3;
    Long Double nVal = BinToDbl(DblToBin(iVal));

    Then comparing iVal vs nVal.

    Thanks again for the help so far.

  5. #5
    Join Date
    Aug 2006
    Posts
    157

    Re: Double to Binary Array for IO output

    The following works for me...

    Code:
    void DblToBin(long double iVal, unsigned char* vPtr)
    {
       memcpy(vPtr ,&iVal, 8);
    }
    
    long double BinToDbl(unsigned char* vPtr)
    {
       long double iVal = 0.0;
       memcpy(&iVal ,&vPtr[0], 8);
       return iVal;
    }
    then...

    Code:
       double d1 = 2345.0;
       unsigned char pc[8];
       DblToBin(d1, pc);
       double d2 = BinToDbl(pc);
    Last edited by sockman; June 18th, 2008 at 09:25 PM.

  6. #6
    Join Date
    Jun 2008
    Posts
    10

    Re: Double to Binary Array for IO output

    This is driving me crazy, I copied your exact functions and test example.

    double d1 = 2345.0;
    unsigned char pc[8];
    DblToBin(d1, pc);
    double d2 = BinToDbl(pc);
    Debug_Output("From " + DoubleToString(d1) + " to " + DoubleToString(d2));

    Output was: "From 2345 to 0"

    What compiler and OS are you running?

  7. #7
    Join Date
    Aug 2006
    Posts
    157

    Re: Double to Binary Array for IO output

    vs2005 and xp.

    Can you please post your complete code?

  8. #8
    Join Date
    Jun 2008
    Posts
    10

    Re: Double to Binary Array for IO output

    I'm using Dev C++ on XP which is probably the issue.

    I dont think the rest or my code will make a difference as Double to String returns the correct value for the first d1. Debug_Output works 100% as I use it everywhere.

    These are the functions you gave me:

    void DblToBin(long double iVal, unsigned char* vPtr);
    long double BinToDbl(unsigned char* vPtr);

    void DblToBin(long double iVal, unsigned char* vPtr)
    {
    memcpy(vPtr ,&iVal, 8);
    }

    long double BinToDbl(unsigned char* vPtr)
    {
    long double iVal = 0.0;
    memcpy(&iVal ,&vPtr[0], 8);
    return iVal;
    }

    And this is exactly what I'm calling:


    double d1 = 2345.0;
    unsigned char pc[8];
    DblToBin(d1, pc);
    double d2 = BinToDbl(pc);
    Debug_Output("From " + DoubleToString(d1) + " to " + DoubleToString(d2));

    My Other Functions

    string DoubleToString( double Num)
    {

    std:stringstream oss;
    oss << Num;
    return oss.str();

    }


    void Debug_Output(string sout)
    {
    clock_t newticks;
    newticks = clock() - ticks;

    time_t curtime = time(0);
    tm now=*localtime(&curtime);

    char dest[100]={0};
    const char format[]="%Y-%m-%d %X - ";
    string stime;

    if (strftime(dest, sizeof(dest)-1, format, &now)>0)
    {
    stime.append( dest);
    }

    string path=datapath;
    path = Prep_Str(path);

    sout = stime + " " + DoubleToString(newticks) + " " + sout;
    sout.append("\n");

    path.append("debug.csv");
    ofstream cOutput ( path.c_str(), ios::app );
    cOutput << sout.c_str() ;
    cOutput.close();

    ticks = clock();
    return ;
    }


    If it makes a difference I'm compiling to a DLL instead of Exe. My whole project runs nicely from a non binary load function but I really need it to be faster still.

    Thanks for your help so far.

    *Complete code would be 1800 lines.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Double to Binary Array for IO output

    Quote Originally Posted by Kaikyro
    I'm using Dev C++ on XP which is probably the issue.
    Why do you say that this is the issue? Dev C++ uses g++ 3.x underneath, and I see no problems using this compiler (unless it is using something *very* esoteric in terms of the C++ language).
    I dont think the rest or my code will make a difference
    So if there is a problem with the code you didn't post, no one will know about it.
    *Complete code would be 1800 lines.
    Then reproduce the problem with a smaller example. By saying that your code is 1800 lines, then it is more likely that the problem is something you're doing, and not a compiler issue.

    Second, use code tags when posting code. Your code is not properly formatted and is hard to read.
    Code:
    void DblToBin(long double iVal, unsigned char* vPtr)
    {
       memcpy(vPtr ,&iVal, 8);
    }
    This code is not portable. The number of bytes a double uses is sizeof(double), not 8. Each compiler could implement doubles differently.
    Code:
    void DblToBin(long double iVal, unsigned char* vPtr)
    {
       memcpy(vPtr ,&iVal, sizeof(double));
    }
    
    long double BinToDbl(unsigned char* vPtr)
    {
       long double iVal = 0.0;
       memcpy(&iVal ,&vPtr[0], sizeof(double));
       return iVal;
    }
    
    //...
        
       double d1 = 2345.0;
       unsigned char pc[sizeof(double)];
       DblToBin(d1, pc);
       double d2 = BinToDbl(pc);
       Debug_Output("From " + DoubleToString(d1) + " to " + DoubleToString(d2));
    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Jun 2008
    Posts
    10

    Re: Double to Binary Array for IO output

    Paul you are the single reason I dont usually post questions on forums.
    I'm using Dev C++ on XP which is probably the issue.
    Why do you say that this is the issue? Dev C++ uses g++ 3.x underneath, and I see no problems using this compiler (unless it is using something *very* esoteric in terms of the C++ language).
    I meant the fact that the example was based on a different compiler.

    I dont think the rest or my code will make a difference
    So if there is a problem with the code you didn't post, no one will know about it.
    I posted every piece of code that is used to convert Binary to Double and back. There are no external dependencies therefore no point filling a forum with irrelevant lines of code.

    *Complete code would be 1800 lines.
    Then reproduce the problem with a smaller example. By saying that your code is 1800 lines, then it is more likely that the problem is something you're doing, and not a compiler issue.
    What is your problem man? In my 3rd post I said I copied his example and ran that with the problem still. I compiled a blank project with the example code, and produced the error still.

    The last tips were useful but were very dragged down by the first half of your post. I dislike you and believe you are what stop people from asking for help in good communities like this.

    I am apologetic to everyone else reading this thread. I really would love a solution otherwise I'll likely be working on this error for weeks. sockman I feel like were very close to having it working. Can someone try this on Dev-CPP and see if it is just me?

  11. #11
    Join Date
    Jun 2008
    Posts
    10

    Re: Double to Binary Array for IO output

    I found the problem.

    It was (as I stated above) a difference in Compilers.

    Where in VS 2005 long Double and Double are the same legnth in Bytes (or something like that) in Bloodshed C++ they are different.

    The fix was simple to convert everything to Double.

    Code:
    void DblToBin(double iVal, unsigned char* vPtr)
    {
       memcpy(vPtr ,&iVal,  sizeof(double));
    }
    
    double BinToDbl(unsigned char* vPtr)
    {
       double iVal = 0.0000;
       memcpy(&iVal ,&vPtr[0],  sizeof(double));
       return iVal;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured