digitalmars.D.learn - Read Byte Array to Integer
- Jeroen Bollen (8/8) Nov 22 2013 How do I read a byte array into an unsigned integer using little
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/18) Nov 22 2013 It looks like you need to include std.system for Endian's definition:
- Jeroen Bollen (2/28) Nov 22 2013 I have std.system included... :s
- monarch_dodra (2/14) Nov 22 2013 What is the type of your "data"?
- Jeroen Bollen (2/18) Nov 22 2013 immutable ubyte[]
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (18/35) Nov 22 2013 That means that the slice itself cannot be modified, meaning that it
- Jeroen Bollen (2/4) Nov 23 2013 Why does read need to be able to change the byte array?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/10) Nov 23 2013 From the documentation: "The T.sizeof bytes which are read are consumed...
- Jeroen Bollen (7/20) Nov 23 2013 So if I have a byte array [0, 0, 1, 0], and I read a ushort from
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/9) Nov 23 2013 Yes, that's the idea.
- monarch_dodra (19/28) Nov 22 2013 I was going to suggest using a "raw" "formattedRead", eg "%-r",
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
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
On Friday, 22 November 2013 at 19:22:16 UTC, Ali Çehreli wrote:On 11/22/2013 11:10 AM, Jeroen Bollen wrote:I have std.system included... :sHow 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
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:What is the type of your "data"?import std.bitmanip; import std.system; void main() { ubyte[] data = [ 1, 2, 3, 4 ]; assert(data.read!(uint, Endian.littleEndian) == 0x04030201); } AliI have std.system included... :s
Nov 22 2013
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:immutable ubyte[]On Friday, 22 November 2013 at 19:22:16 UTC, Ali Çehreli wrote:What is the type of your "data"?import std.bitmanip; import std.system; void main() { ubyte[] data = [ 1, 2, 3, 4 ]; assert(data.read!(uint, Endian.littleEndian) == 0x04030201); } AliI have std.system included... :s
Nov 22 2013
On 11/22/2013 02:55 PM, Jeroen Bollen wrote:> On Friday, 22 November 2013 at 21:17:56 UTC, monarch_dodra wrote: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); } AliOn Friday, 22 November 2013 at 19:44:56 UTC, Jeroen Bollen wrote:immutable ubyte[]On Friday, 22 November 2013 at 19:22:16 UTC, Ali Çehreli wrote:What is the type of your "data"?import std.bitmanip; import std.system; void main() { ubyte[] data = [ 1, 2, 3, 4 ]; assert(data.read!(uint, Endian.littleEndian) == 0x04030201); } AliI have std.system included... :s
Nov 22 2013
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
On 11/23/2013 04:08 AM, Jeroen Bollen wrote:On Friday, 22 November 2013 at 23:12:25 UTC, Ali Çehreli wrote: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. AliThat 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
On Saturday, 23 November 2013 at 15:21:02 UTC, Ali Çehreli wrote:On 11/23/2013 04:08 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; // == 0On Friday, 22 November 2013 at 23:12:25 UTC, Ali Çehreli wrote: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. AliThat 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
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; // == 0Yes, that's the idea. Ali
Nov 23 2013
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