www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Read Byte Array to Integer

reply "Jeroen Bollen" <jbinero gmail.com> writes:
How do I read a byte array into an unsigned integer using little 
endian formatting? I know about the .read inside the std.bitmanip 
but I can't fingure out how to set it to little endian.

data.read!(uint, Endian.littleEndian)();

This gives me a huge compile message including the error at the 
top (without the candidates)

Error: template std.bitmanip.read does not match any function 
template declaration.
Nov 22 2013
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/22/2013 11:10 AM, Jeroen Bollen wrote:
 How do I read a byte array into an unsigned integer using little endian
 formatting? I know about the .read inside the std.bitmanip but I can't
 fingure out how to set it to little endian.

 data.read!(uint, Endian.littleEndian)();

 This gives me a huge compile message including the error at the top
 (without the candidates)

 Error: template std.bitmanip.read does not match any function template
 declaration.
It looks like you need to include std.system for Endian's definition: import std.bitmanip; import std.system; void main() { ubyte[] data = [ 1, 2, 3, 4 ]; assert(data.read!(uint, Endian.littleEndian) == 0x04030201); } Ali
Nov 22 2013
parent reply "Jeroen Bollen" <jbinero gmail.com> writes:
On Friday, 22 November 2013 at 19:22:16 UTC, Ali Çehreli wrote:
 On 11/22/2013 11:10 AM, Jeroen Bollen wrote:
 How do I read a byte array into an unsigned integer using 
 little endian
 formatting? I know about the .read inside the std.bitmanip but 
 I can't
 fingure out how to set it to little endian.

 data.read!(uint, Endian.littleEndian)();

 This gives me a huge compile message including the error at 
 the top
 (without the candidates)

 Error: template std.bitmanip.read does not match any function 
 template
 declaration.
It looks like you need to include std.system for Endian's definition: 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
Nov 22 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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"?
Nov 22 2013
parent reply "Jeroen Bollen" <jbinero gmail.com> writes:
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[]
Nov 22 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Nov 22 2013
parent reply "Jeroen Bollen" <jbinero gmail.com> writes:
On Friday, 22 November 2013 at 23:12:25 UTC, Ali Çehreli wrote:
 That means that the slice itself cannot be modified, meaning 
 that it cannot be consumed by read. Can't work... :)
Why does read need to be able to change the byte array?
Nov 23 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/23/2013 04:08 AM, Jeroen Bollen wrote:
 On Friday, 22 November 2013 at 23:12:25 UTC, Ali Çehreli wrote:
 That means that the slice itself cannot be modified, meaning that it
 cannot be consumed by read. Can't work... :)
Why does read need to be able to change the byte array?
From the documentation: "The T.sizeof bytes which are read are consumed from the range." When it comes to arrays, only slices that support popFront() are ranges. Ali
Nov 23 2013
parent reply "Jeroen Bollen" <jbinero gmail.com> writes:
On Saturday, 23 November 2013 at 15:21:02 UTC, Ali Çehreli wrote:
 On 11/23/2013 04:08 AM, Jeroen Bollen wrote:
 On Friday, 22 November 2013 at 23:12:25 UTC, Ali Çehreli wrote:
 That means that the slice itself cannot be modified, meaning 
 that it
 cannot be consumed by read. Can't work... :)
Why does read need to be able to change the byte array?
From the documentation: "The T.sizeof bytes which are read are consumed from the range." When it comes to arrays, only slices that support popFront() are ranges. Ali
So if I have a byte array [0, 0, 1, 0], and I read a ushort from it twice, I will get this? ubyte[] arr = [0, 0, 1, 0]; arr.read!(ushort, Endian.littleEndian); // == 0 arr.read!(ushort, Endian.littleEndian); // == 1 arr.length; // == 0
Nov 23 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/23/2013 08:36 AM, Jeroen Bollen wrote:

 So if I have a byte array [0, 0, 1, 0], and I read a ushort from it
 twice, I will get this?

 ubyte[] arr = [0, 0, 1, 0];
 arr.read!(ushort, Endian.littleEndian); // == 0
 arr.read!(ushort, Endian.littleEndian); // == 1
 arr.length; // == 0
Yes, that's the idea. Ali
Nov 23 2013
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Friday, 22 November 2013 at 19:10:24 UTC, Jeroen Bollen wrote:
 How do I read a byte array into an unsigned integer using 
 little endian formatting? I know about the .read inside the 
 std.bitmanip but I can't fingure out how to set it to little 
 endian.

 data.read!(uint, Endian.littleEndian)();

 This gives me a huge compile message including the error at the 
 top (without the candidates)

 Error: template std.bitmanip.read does not match any function 
 template declaration.
I was going to suggest using a "raw" "formattedRead", eg "%-r", but apparently, that doesn't work for reading, only writing :/ //---- import std.format; import std.stdio; void main() { int i = 0x04_03_02_01; auto app = appender!string(); formattedWrite(app, "%-r", i); string s = app.data; writeln(cast(ubyte[])s); //Produces [1, 2, 3, 4] formattedRead(s, "%-r", &i); //Fails on - formattedRead(s, "%r", &i); //Fails on r } //---- That would be an interesting improvement. Raw formatted write is so awesome, raw formatted read would also be very cool.
Nov 22 2013