How do I cast to from byte[] <-> double for making a small assembler language VM to work?

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 18 09:17:34 PDT 2017


Maybe use a union?

	union U {
		double d;
		byte[double.sizeof] bytes;
	}

	U u;
	u.bytes = ...;
	double d = u.d;
	... // do something with d

	// or:
	U u;
	u.d = 3.14159;
	byte[] b = u.bytes[];
	... // do something with b

Casting a pointer may run into alignment issues, if your byte[] isn't
aligned to a double.


T

-- 
Only boring people get bored. -- JM


More information about the Digitalmars-d-learn mailing list