Jump to page: 1 2
Thread overview
Read Byte Array to Integer
Nov 22, 2013
Jeroen Bollen
Nov 22, 2013
Ali Çehreli
Nov 22, 2013
Jeroen Bollen
Nov 22, 2013
monarch_dodra
Nov 22, 2013
Jeroen Bollen
Nov 22, 2013
Ali Çehreli
Nov 23, 2013
Jeroen Bollen
Nov 23, 2013
Ali Çehreli
Nov 23, 2013
Jeroen Bollen
Nov 23, 2013
Ali Çehreli
Nov 22, 2013
monarch_dodra
November 22, 2013
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.
November 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

November 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:
>> 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
November 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:
>> 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"?
November 22, 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.
November 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:
>> 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[]
November 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:
>> 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

November 23, 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?
November 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:
>> 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."

  http://dlang.org/phobos/std_bitmanip.html#.read

When it comes to arrays, only slices that support popFront() are ranges.

Ali

November 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:
>> 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."
>
>   http://dlang.org/phobos/std_bitmanip.html#.read
>
> 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
« First   ‹ Prev
1 2