How to serialize a double.
H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Nov 30 16:45:19 PST 2016
On Thu, Dec 01, 2016 at 12:36:30AM +0000, Jake Pittis via Digitalmars-d-learn wrote:
> How do I convert a double to a ubyte[]?
>
> I've tried all sorts of things including converting the double to a
> ulong and trying to serialize the ulong. For example test bellow
> fails.
>
> ````
> unittest {
> double d = 3.14;
> ulong l = *cast(ulong*)(&d);
> double after = *cast(double*)(&l));
> assert(after == d); // This fails.
> }
> ````
union U
{
ubyte[double.sizeof] bytes;
double d;
}
U u, v;
u.d = 3.14159;
v.bytes[] = u.bytes[];
assert(v.d == 3.14159);
T
--
Those who don't understand Unix are condemned to reinvent it, poorly.
More information about the Digitalmars-d-learn
mailing list