digitalmars.D.learn - How to convert byte array to float
- DLangLearner (4/4) Jul 17 2015 Excuse me for my trivial question, but I'd like to know how to
- byron (9/13) Jul 17 2015 You want to include [] in the cast so:
- byron (6/19) Jul 17 2015 Ah I miss read, if you want as a float, not a float array you can
- Jacob Carlborg (4/8) Jul 17 2015 I think a union can be used as well, not sure if it's better though.
- byron (14/24) Jul 17 2015 not a bad idea.
- DLangLearner (2/28) Jul 17 2015 thanks for quick reply, i'll plug above lines into my codes.
- Jonathan M Davis via Digitalmars-d-learn (5/9) Jul 17 2015 You could use std.bitmanip.peek or std.bitmanip.read - though you do nee...
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!
Jul 17 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];
Jul 17 2015
On Friday, 17 July 2015 at 18:53:24 UTC, byron wrote:On Friday, 17 July 2015 at 18:43:27 UTC, DLangLearner wrote:Ah I miss read, if you want as a float, not a float array you can do: byte[] b = [1, 2, 3, 4]; float f = *cast(float*)b.ptr; not sure if there is a better wayExcuse 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];
Jul 17 2015
On 2015-07-17 20:58, byron wrote:Ah I miss read, if you want as a float, not a float array you can do: byte[] b = [1, 2, 3, 4]; float f = *cast(float*)b.ptr; not sure if there is a better wayI think a union can be used as well, not sure if it's better though. -- /Jacob Carlborg
Jul 17 2015
On Friday, 17 July 2015 at 19:03:46 UTC, Jacob Carlborg wrote:On 2015-07-17 20:58, byron wrote:not a bad idea. testing on asm.dlang.org both reduce to the same assembly (-release -inline -O) float pointerTest(inout byte[4] b) { return *cast(float*)b.ptr; } float unionTest(inout byte[4] b) { union U { byte[4] b; float f; } return U(b).f; }Ah I miss read, if you want as a float, not a float array you can do: byte[] b = [1, 2, 3, 4]; float f = *cast(float*)b.ptr; not sure if there is a better wayI think a union can be used as well, not sure if it's better though.
Jul 17 2015
On Friday, 17 July 2015 at 18:58:33 UTC, byron wrote:On Friday, 17 July 2015 at 18:53:24 UTC, byron wrote:thanks for quick reply, i'll plug above lines into my codes.On Friday, 17 July 2015 at 18:43:27 UTC, DLangLearner wrote:Ah I miss read, if you want as a float, not a float array you can do: byte[] b = [1, 2, 3, 4]; float f = *cast(float*)b.ptr; not sure if there is a better wayExcuse 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];
Jul 17 2015
On Friday, July 17, 2015 18:43:26 DLangLearner via Digitalmars-d-learn 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 could use std.bitmanip.peek or std.bitmanip.read - though you do need to make sure that you're telling it the right endianness, depending on how the array of bytes was generated. - Jonathan M Davis
Jul 17 2015