bitmanip bigEndianToNative using a buffer slice?
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Oct 21 18:59:20 PDT 2014
On Wednesday, October 22, 2014 00:45:17 Lucas Burson via
Digitalmars-d-learn
wrote:
> I'm trying to create a primitive type given a specific buffer
> slice. I can place the uint into a sliced buffer but I'm getting
> compiler errors when using a slice to create the uint. Still new
> to Dlang and unfamiliar with the template system.
>
> How do I get this working?
>
> import std.bitmanip;
> int main()
> {
> size_t offset = 3;
> ubyte[10] buffer;
> buffer[offset..offset+4] = nativeToBigEndian!uint(cast(uint)
> 104387);
>
> // compiler error
> uint fromBuf =
> bigEndianToNative!uint(buffer[offset..offset+4]);
> return 0;
> }
>
> The compiler error:
> ./test.d(11): Error: template std.bitmanip.bigEndianToNative
> does
> not match any function template declaration. Candidates are:
> /usr/include/dmd/phobos/std/bitmanip.d(1689):
> std.bitmanip.bigEndianToNative(T, ulong n)(ubyte[n] val) if
> (canSwapEndianness!(T) && n == T.sizeof)
> ./test.d(11): Error: template std.bitmanip.bigEndianToNative(T,
> ulong n)(ubyte[n] val) if (canSwapEndianness!(T) && n ==
> T.sizeof) cannot deduce template function from argument types
> !(uint)(ubyte[])
> ./test.d(11): Error: template instance bigEndianToNative!(uint)
> errors instantiating template
You can't just use a dynamic array as a static array, because
they're distinct
types. Slicing a static array gives you a dynamic one which
refers to the
static array's memory, but to convert a dynamic array to a static
one, you
have to cast it (which will do a copy). So, your code becomes
something like
import std.bitmanip;
void main()
{
size_t offset = 3;
ubyte[10] buffer;
buffer[offset..offset+4] = nativeToBigEndian!uint(104387);
// compiler error
auto fromBuf =
bigEndianToNative!uint(cast(ubyte[4])buffer[offset..offset+4]);
}
However, in this case, you should probably just not use a dynamic
array. It
buys you nothing. You might as well just do.
import std.bitmanip;
void main()
{
auto buffer = nativeToBigEndian!uint(104387);
// compiler error
auto fromBuf = bigEndianToNative!uint(buffer);
}
though obviously, your actual code may be doing something more
complicated
that actually makes using the dynamic array reasonable.
Regardless, dynamic
arrays must be cast to static arrays if you want to use a dynamic
array where
a static array is required.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list