|
-
June 18th, 2008, 06:34 PM
#1
[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.
-
June 18th, 2008, 07:21 PM
#2
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.
-
June 18th, 2008, 07:21 PM
#3
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;
}
-
June 18th, 2008, 07:37 PM
#4
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.
-
June 18th, 2008, 09:22 PM
#5
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.
-
June 18th, 2008, 09:52 PM
#6
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?
-
June 18th, 2008, 10:46 PM
#7
Re: Double to Binary Array for IO output
vs2005 and xp.
Can you please post your complete code?
-
June 18th, 2008, 11:16 PM
#8
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.
-
June 19th, 2008, 12:29 AM
#9
Re: Double to Binary Array for IO output
 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
-
June 19th, 2008, 04:35 AM
#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?
-
June 19th, 2008, 04:41 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|