www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to convert ubyte[] to uint?

reply Dr.No <jckj33 gmail.com> writes:
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;
May 23 2018
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
May 23 2018
parent reply Dr.No <jckj33 gmail.com> writes:
On Wednesday, 23 May 2018 at 19:49:27 UTC, Jonathan M Davis wrote:
 On Wednesday, May 23, 2018 19:36:07 Dr.No via 
 Digitalmars-d-learn wrote:
       [...]
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
sorry, the error message wasn't clear to me. When I use dynamic arrays I get: slice of static array temporary returned by fnv64.finish() assigned to longer lived variable arr What should use instead of?
May 23 2018
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/23/18 3:53 PM, Dr.No wrote:
 On Wednesday, 23 May 2018 at 19:49:27 UTC, Jonathan M Davis wrote:
 On Wednesday, May 23, 2018 19:36:07 Dr.No via Digitalmars-d-learn wrote:
       [...]
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
sorry, the error message wasn't clear to me. When I use dynamic arrays I get: slice of static array temporary returned by fnv64.finish() assigned to longer lived variable arr What should use instead of?
I'm guessing you wrote: ubyte[] arr = fnv64.finish(); ?? You want: auto arrtmp = fnv64.finish(); auto arr = arrtmp[]; Basically, what you were doing is allocating some stack space to hold a static array that immediately goes out of scope, and then storing a slice to it. -Steve
May 23 2018