문제

I am looking for a way of getting the result of this procedure in C#. To be precise, I am trying to unpack data from a binary string, according to the given format (the method should return an array). Please refer to http://docs.python.org/2/library/struct.html for more information.

struct.unpack('!hh', data[2:6])

Is there a way of doing such in C#?

도움이 되었습니까?

해결책

I think BinaryReader is what you want.

Something like this would be the equivalent (assuming data is a byte array):

using (var stream = new MemoryStream(data)) {
    using (var reader = new BinaryReader(stream)) {
        stream.Seek(2, SeekOrigin.Begin);

        var value1 = reader.ReadInt16();
        var value2 = reader.ReadInt16();
    }
}

This should skip the first 2 bytes, then load the next 4 bytes into two short variables.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top