Read Byte Array to Integer

Ali Çehreli acehreli at yahoo.com
Fri Nov 22 15:12:24 PST 2013


On 11/22/2013 02:55 PM, Jeroen Bollen wrote:> On Friday, 22 November 
2013 at 21:17:56 UTC, monarch_dodra wrote:
 >> On Friday, 22 November 2013 at 19:44:56 UTC, Jeroen Bollen wrote:
 >>> On Friday, 22 November 2013 at 19:22:16 UTC, Ali Çehreli wrote:
 >>>> import std.bitmanip;
 >>>> import std.system;
 >>>>
 >>>> void main()
 >>>> {
 >>>>   ubyte[] data = [ 1, 2, 3, 4 ];
 >>>>   assert(data.read!(uint, Endian.littleEndian) == 0x04030201);
 >>>> }
 >>>>
 >>>> Ali
 >>>
 >>> I have std.system included... :s
 >>
 >> What is the type of your "data"?
 >
 > immutable ubyte[]

That means that the slice itself cannot be modified, meaning that it 
cannot be consumed by read. Can't work... :)

This would work though:

   immutable(ubyte)[]

However, if you really want your main data to be immutable, you can keep 
it as 'immutable ubyte[]' but you must take an lvalue slice of it to be 
passed to read:

import std.bitmanip;
import std.system;

void main()
{
     immutable ubyte[] mainData = [ 1, 2, 3, 4 ];
     auto dataSlice = mainData[];
     assert(dataSlice.read!(uint, Endian.littleEndian) == 0x04030201);
}

Ali



More information about the Digitalmars-d-learn mailing list