use*_*843 3 bytebuffer d bytearray read-write
我今天刚开始学习D,我真的需要读取和写入这样的数据:
byte[] bytes = ...;
ByteBuffer buf = new ByteBuffer(bytes);
int a = buf.getInt();
byte b = buf.getByte();
short s = buf.getShort();
buf.putInt(200000);
Run Code Online (Sandbox Code Playgroud)
D中有什么内容可以实现这一点,还是我必须自己制作?
我建议在看std.bitmanip的read, peek,write和append功能.例如
ubyte[] buffer = [1, 5, 22, 9, 44, 255, 8];
auto range = buffer; // if you don't want to mutate the original
assert(range.read!ushort() == 261);
assert(range == [22, 9, 44, 255, 8]);
assert(range.read!uint() == 369_700_095);
assert(range == [8]);
assert(range.read!ubyte() == 8);
assert(range.empty);
assert(buffer == [1, 5, 22, 9, 44, 255, 8]);
Run Code Online (Sandbox Code Playgroud)
没有缓冲区类型 - 而是它们是在ubyte(包括ubyte[])范围内运行的自由函数- 因此它们可能无法完全像您正在寻找的那样工作,但它们是为您需要提取整数值的情况而设计的从数组或其他类型的字节范围.如果你真的想要某种单独的缓冲区类型,那么你应该能够很容易地创建一个在内部使用它们.