How to convert byte array to float

byron via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 17 11:53:20 PDT 2015


On Friday, 17 July 2015 at 18:43:27 UTC, DLangLearner wrote:
> Excuse me for my trivial question, but I'd like to know how to 
> convert byte array to float? What I could think of are 
> cast(float)(byte[]) and to!float(byte[]) but they do not work 
> for me. Thanks!

You want to include [] in the cast so:



byte[] b = [1, 2, 3, 4]
byte[] f = cast(float[])b;

writeln(b);
writeln(f);

You can even cast on a slice:

byte[] b = [1, 2, 3, 4, 5, 6, 7, 8]
byte[] f = cast(float[])b[4..8];



More information about the Digitalmars-d-learn mailing list