Read file/stream

Ali Çehreli acehreli at yahoo.com
Fri Mar 11 11:50:05 PST 2011


On 03/11/2011 11:18 AM, Stewart Gordon wrote:
> On 11/03/2011 18:46, Steven Schveighoffer wrote:
> <snip>
>> I am not sure what facilities Phobos provides for reading/writing
>> integers in network
>> order (i.e. Big Endian), but I'm sure there's something.
>
> http://www.digitalmars.com/d/1.0/phobos/std_stream.html
> EndianStream
>
> I haven't experimented with it. And I don't expect it to handle structs
> well. Alternatively, you could use some simple code like
>
> --------
> version (BigEndian) {
> uint bigEndian(uint value) {
> return value;
> }
> }
>
> version (LittleEndian) {
> uint bigEndian(uint value) {
> return value << 24
> | (value & 0x0000FF00) << 8
> | (value & 0x00FF0000) >> 8
> | value >> 24;
> }
> }

There is also std.intrinsic.bswap

Ali

> --------
>
> though you would have to remember to call it for each file I/O operation
> that relies on it. If you use a struct, you could put a method in it to
> call bigEndian on the members of relevance.
>
> Stewart.



More information about the Digitalmars-d-learn mailing list