How to convert ubyte[] to uint?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed May 23 19:49:27 UTC 2018


On Wednesday, May 23, 2018 19:36:07 Dr.No via Digitalmars-d-learn wrote:
> read fails with both uint and ulong on 64bit platform:
>
>   Error: template std.bitmanip.read cannot deduce function from
> argument types !(ulong)(ubyte[8]), candidates are:
> C:\ldc2-1.9.0-windows-x64\bin\..\import\std\bitmanip.d(3213,3):
>       std.bitmanip.read(T, Endian endianness = Endian.bigEndian,
> R)(ref R range) if (canSwapEndianness!T && isInputRange!R &&
> is(ElementType!R : const(ubyte)))
>
> code:
>
>       import digestx.fnv;
>       import std.bitmanip : read;
>       FNV64 fnv64;
>       fnv64.start();
>       fnv64.put(cast(ubyte[])word);
>       ubyte[8] arr = fnv64.finish();
>       auto h = arr.read!ulong;
>       return cast(uint)h;

As the template constraint in the error message says, read requires an input
range. Static arrays are not input ranges. You need to give it a dynamic
array - and since read takes its argument by reference, you can't simply
slice the static array and pass it. You need a variable that's a dynamic
array.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list