Read Byte Array to Integer

monarch_dodra monarchdodra at gmail.com
Fri Nov 22 13:24:15 PST 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.


More information about the Digitalmars-d-learn mailing list