# Include <istream>
# Include <iostream>
# Include <fstream>
# Define packetrecv_size 20
Typedef char int8;
Typedef unsigned char uint8;
Typedef signed int int32;
Typedef unsigned int uint32;
Using namespace STD;
Uint8 _ buff [packetrecv_size] = {0xad, 0x83, 0xce, 0xb4, 0x5d, 0xf3, 0x40, 0x97, 0xf0, 0x70,
0x8e, 0x23, 0xa4, 0x56, 0xb0, 0x72, 0x9b, 0x77,0x90, 0xcb };
Uint8 buffer [2] = {0x40, 0x5d };
Class memorystreambuf: public STD: streambuf {
Public:
Memorystreambuf (char * pbuffer, uint32 buffersize );
Public:
Uint32 _ written;
Char * _ pbuffer;
Uint32 _ buffersize;
};
Memorystreambuf: memorystreambuf (char * pbuffer, uint32 buffersize): _ pbuffer (pbuffer), _ buffersize (buffersize), _ written (0 ){
Setg (_ pbuffer, _ pbuffer, _ pbuffer + _ buffersize );
Setp (_ pbuffer, _ pbuffer + _ buffersize );
}
Class memoryios: Public Virtual STD: IOS
{
Public:
Memoryios (char * pbuffer, uint32 buffersize );
~ Memoryios (){}
Memorystreambuf * rdsbuf (){
Return & _ Buf;
}
Public:
Memorystreambuf _ Buf;
};
Memoryios: memoryios (char * pbuffer, uint32 buffersize): _ Buf (pbuffer, buffersize ){
}
Class memoryinputstream: Public memoryios, public STD: istream
{
Public:
Memoryinputstream (const char * pbuffer, uint32 buffersize );
};
Memoryinputstream: memoryinputstream (const char * pbuffer, uint32 buffersize ):
Memoryios (const_cast <char *> (pbuffer), buffersize), istream (rdsbuf ())
{}
Class binaryreader {
Public:
Binaryreader (STD: istream & istr );
Void read (uint8 & value );
Void read (int8 & value );
Public:
STD: istream & _ istr;
};
Binaryreader: binaryreader (STD: istream & istr ):
_ Istr (istr ){}
Void binaryreader: Read (uint8 & Value ){
_ Istr. Read (char *) & Value, sizeof (value ));
}
Void binaryreader: Read (int8 & Value ){
_ Istr. Read (& Value, sizeof (value ));
}
Int main (){
Memoryinputstream _ memory (const char *) buffer, 2 );
Binaryreader reader (_ memory );
Unsigned char a = 0;
Unsigned char B = 0;
Unsigned char D = 0;
If (reader. _ istr. Read (char *) & A, 1). Good ()){
Printf ("0x %. 2x \ n", );
}
If (reader. _ istr. Read (char *) & B, 1). Good ()){
Printf ("0x %. 2x \ n", B );
}
If (reader. _ istr. Read (char *) & d, 1). Good ()){
Printf ("0x %. 2x \ n", d );
}
Return 0;
}
Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8592857