Click to See Complete Forum and Search --> : Getting double value from a sequence of bytes
LethaLIdea
January 22nd, 2008, 04:32 PM
Hello,
I have a sequence of bytes, stored in either a managed or unmanaged array. There are 8 bytes and they represent a double value.
The question is: how do I get the double value out of the byte sequence? I do NOT want to use the BitConverter method or any other managed methods. I want to use low-level bitshifting and whatever else operation to get the double value of the 8-byte sequence, because it needs to be fast and all the managed methods I have tried are extremely slow. The bytes are also ordered Big Endian, and so would need flipped (the simplest of what I want to do here).
Thanks!
MrViggy
January 22nd, 2008, 04:42 PM
You could put the array in a union with a double. Do your "bit flipping" on the array portion, then just access the actual number as the double member. Or, does this need to be platform independent?
Viggy
LethaLIdea
January 22nd, 2008, 05:30 PM
Hi Viggy,
No that would work and I tried doing that actually...but honestly I didn't know what I was doing and I think thats why "I" couldnt get it to work.
Mind posting some code?
Thanks!
LethaLIdea
January 22nd, 2008, 05:55 PM
Ah I got it, thanks again.
double JPFitsImage::SwapDouble( char b[] )
{
union
{
double f;
char b[8];
} dat1, dat2;
dat1.b[0] = b[0];dat1.b[1] = b[1];dat1.b[2] = b[2];dat1.b[3] = b[3];dat1.b[4] = b[4];dat1.b[5] = b[5];dat1.b[6] = b[6];dat1.b[7] = b[7];
dat2.b[0] = dat1.b[7];
dat2.b[1] = dat1.b[6];
dat2.b[2] = dat1.b[5];
dat2.b[3] = dat1.b[4];
dat2.b[4] = dat1.b[3];
dat2.b[5] = dat1.b[2];
dat2.b[6] = dat1.b[1];
dat2.b[7] = dat1.b[0];
return dat2.f;
}
S_M_A
January 22nd, 2008, 05:58 PM
I don't know of any faster swapping then "just do it" so something like this maybe works?char buf[8];
char tmp;
double value;
tmp = buf[7]; buf[7] = buf[0]; buf[7] = tmp;
tmp = buf[6]; buf[6] = buf[1]; buf[6] = tmp;
tmp = buf[5]; buf[5] = buf[2]; buf[5] = tmp;
tmp = buf[4]; buf[4] = buf[3]; buf[4] = tmp;
value = *(double*)buf;or of course create functions, loops and so on so it becomes nice and readable and without affecting original buffer.
LethaLIdea
January 22nd, 2008, 06:04 PM
a little cleaner....still works
double JPFitsImage::SwapDouble( char b[] )
{
union
{
double f;
char b[8];
} dat2;
dat2.b[0] = b[7];
dat2.b[1] = b[6];
dat2.b[2] = b[5];
dat2.b[3] = b[4];
dat2.b[4] = b[3];
dat2.b[5] = b[2];
dat2.b[6] = b[1];
dat2.b[7] = b[0];
return dat2.f;
}
LethaLIdea
January 22nd, 2008, 06:09 PM
I don't know of any faster swapping then "just do it" so something like this maybe works?char buf[8];
...
value = *(double*)buf;or of course create functions, loops and so on so it becomes nice and readable and without affecting original buffer.
Hmm, just tried that and it really slowed things down. The union method doesn't seem to increase processing time beyond what it would be without doing anything to the data at all, as it is scanned through. So thats fast enough for me...though I'll have to try it on my home system to know for sure cause everything is fast on this one...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.