May 12, 2014 newbie question about variables in slices.. | ||||
---|---|---|---|---|
| ||||
Hi I am trying to iterate over a mmfile (ubyte[]) and convert it to uint void main(){ MmFile inn = new MmFile("mmData.dat"); ubyte[] arr = cast(ubyte[])inn[]; for(ulong index = 0; index<arr.length; index+=4){ ulong stop = index+4; uint num = littleEndianToNative!uint(arr[index..stop]); } if i try to compile this i get the following error: Error: template std.bitmanip.littleEndianToNative cannot deduce function from argument types !(uint)(ubyte[]) but if change the last line to: uint num = littleEndianToNative!uint(arr[30..34]); then it compiles and runs... Am I doing something wrong with my variables "index" and "stop"? cheers Kai T |
May 12, 2014 Re: newbie question about variables in slices.. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kai | On Mon, 12 May 2014 20:12:41 +0000 Kai via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > Hi I am trying to iterate over a mmfile (ubyte[]) and convert it > to uint > > void main(){ > MmFile inn = new MmFile("mmData.dat"); > ubyte[] arr = cast(ubyte[])inn[]; > for(ulong index = 0; index<arr.length; index+=4){ > ulong stop = index+4; > uint num = > littleEndianToNative!uint(arr[index..stop]); } > if i try to compile this i get the following error: > Error: template std.bitmanip.littleEndianToNative cannot deduce > function from argument types !(uint)(ubyte[]) > > but if change the last line to: > uint num = littleEndianToNative!uint(arr[30..34]); > > then it compiles and runs... > > Am I doing something wrong with my variables "index" and "stop"? cheers The problem is that the compiler isn't smart enough to realize that arr[index .. stop] is guaranteed to result in a array with a length of 4. auto num = littleEndianToNative!uint(cast(ubyte[4])arr[index..stop]); would work. On a side note, if you wanted to be safer, you should probably use uint.sizeof everyewhere instead of 4. that would also make it easier to convert it to a different integral type. Also, you should be using size_t, not ulong for the indices. Array indices are size_t, and while that's ulong on a 64-bit system, it's uint on a 32-bit system, so your code won't compile on a 32-bit system. - Jonathan M Davis |
Copyright © 1999-2021 by the D Language Foundation