开发者

ReadInt32, BinaryReader, c++

I have a binary byte array. In C#, it's very easy to read it with BinaryReader and ReadInt32 (that's what I need). like this:

reader = new B开发者_Go百科inaryReader ( new MemoryStream( data ), new UnicodeEncoding() );

m_headerVersion = reader.ReadInt32();
m_width         = reader.ReadInt32();
m_height        = reader.ReadInt32();
...

How can I do the same thing in c++ (MFC)? What should I include to do it?

Thanks


As far as I understand you are using unmanaged C++, and you have to write it your self. C++ and C# different languages after all.

But you can do something similar using STL streams.

std::stringstream reader;
reader << data;
reader >> m_headerVersion;
reader >> m_width;
reader >> m_height;


Assuming you have your data in a

char* data;

You can get it into an int32_t by casting it, i.e.:

int32_t m_headerVersion = int32_t(*data);
int32_t m_width  = int32_t(*(data + sizeof(int32_t)));
int32_t m_height  = int32_t(*(data + 2*sizeof(int32_t)));

That's the basic idea. You should be able to wrap this functionality in an easy-to-use stream-style interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜