www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Hrdcoded

reply aerto <cvsae gmx.com> writes:
Hello, and Happy new year. I'm a bit confused.

     uint read()
     {
       assert(pos == 68);

       uint value = 
littleEndianToNative!uint(cast(ubyte[uint.sizeof])buffer[pos..pos+uint.sizeof]);
           pos += uint.sizeof;
           return value;
         }


The above code doesn't work. instead its working perfect when i 
replace buffer[68..68+uint.sizeof] Any thoughts ?
Jan 16 2023
next sibling parent aerto <cvsae gmx.com> writes:
On Monday, 16 January 2023 at 15:16:46 UTC, aerto wrote:
 Hello, and Happy new year. I'm a bit confused.

     uint read()
     {
       assert(pos == 68);

       uint value = 
 littleEndianToNative!uint(cast(ubyte[uint.sizeof])buffer[pos..pos+uint.sizeof]);
           pos += uint.sizeof;
           return value;
         }


 The above code doesn't work. instead its working perfect when i 
 replace buffer[68..68+uint.sizeof] Any thoughts ?
Sorry for the wrong section and the bad title.
Jan 16 2023
prev sibling parent reply Dennis <dkorpel gmail.com> writes:
On Monday, 16 January 2023 at 15:16:46 UTC, aerto wrote:
 The above code doesn't work.
What doesn't work? Can you also give the definitions of the variables and functions involved?
Jan 16 2023
parent reply aerto <cvsae gmx.com> writes:
On Monday, 16 January 2023 at 15:35:38 UTC, Dennis wrote:
 On Monday, 16 January 2023 at 15:16:46 UTC, aerto wrote:
 The above code doesn't work.
What doesn't work? Can you also give the definitions of the variables and functions involved?
class iv{ private byte[] buffer; private ulong writePos; private ulong readPos; this(){} void write(T)(T value) if (is(T == uint)){ buffer ~= nativeToLittleEndian!uint(value); writePos += uint.sizeof; } uint readUint() { assert(readPos == 0); uint value = littleEndianToNative!uint(cast(ubyte[uint.sizeof])buffer[readPos..readPos+uint.sizeof]); readPos += uint.sizeof; return value; } uint readUint_test() { assert(readPos == 0); uint value = littleEndianToNative!uint(cast(ubyte[uint.sizeof])buffer[0..0+uint.sizeof]); readPos += uint.sizeof; return value; } } void main() { auto cc = new iv(); uint ptr = 1000000; cc.write(ptr); writeln(cc.readUint()); //not works writeln(cc.readUint_test()); // works } The readUint_test its working while the readUint not.
Jan 16 2023
parent reply Dennis <dkorpel gmail.com> writes:
On Monday, 16 January 2023 at 15:49:27 UTC, aerto wrote:
 The readUint_test its working while the readUint not.
The compiler currently doesn't determine that `readPos..readPos+4` has length 4, so it doesn't like the static array cast. You can do this instead: ``` littleEndianToNative!uint(cast(ubyte[uint.sizeof])buffer[readPos..$][0..uint.sizeof]); ```
Jan 16 2023
parent aerto <cvsae gmx.com> writes:
On Monday, 16 January 2023 at 16:30:29 UTC, Dennis wrote:
 On Monday, 16 January 2023 at 15:49:27 UTC, aerto wrote:
 The readUint_test its working while the readUint not.
The compiler currently doesn't determine that `readPos..readPos+4` has length 4, so it doesn't like the static array cast. You can do this instead: ``` littleEndianToNative!uint(cast(ubyte[uint.sizeof])buffer[readPos..$][0..uint.sizeof]); ```
ohh. Thank you very much.
Jan 16 2023