c++ - How to feed binary hex literal into std::istream? -


i have method deserialize taking reference opened std::istream pass in std::ifstream opened std::ios::binary option.

now test binary (hex) literals not know how feed data std::istream.

i tried similar in answer

struct membuf : std::streambuf {   membuf(char* begin, char* end)   {     this->setg(begin, begin, end);   } };  int main() {   char buffer[] = "0a0b0c0d000000480000000000420410000";    membuf sbuf(buffer, buffer + sizeof(buffer) - 1);   std::istream in(&sbuf);    deserialize(in); } 

which fails because data not read/fed binary.

how can it?

you don't have binary data in buffer, characters. have plain c-style character literal.

to feed input binary data need declaration this:

unsigned char buffer[] = { 0x0a, 0x0b, 0x0c, 0x0d, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00,                              0x00, 0x00, 0x00, 0x42, 0x04, 0x10, 0x00, 0x00 }; 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -