Thread: Streaming byte arrays

  1. #1
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139

    Streaming byte arrays

    I've been looking for a way to effectively extract variables from data stored in a byte array. I looked at std:stream but the only examples I see used are with the cout object. There seems to be no way to set the underlying source to an array of bytes.

    Right now my code looks like this, and I can imagine it looks rather ugly:

    Code:
    char *p = data+9; // start at byte 10
    char str31[31];
    user->SetName(std::memmove(str31, p, 31)); p += 31;
    char i = *(p++);
    user->SetPassword(std::memmove(str31, p, 31), i); p += 31;
    user->SetAux(*(std::uint32_t*)p); p += 24; // skip 24 bytes of unused junk
    user->SetRoom(*(std::uint32_t*)p); // skip rest

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Chris87
    There seems to be no way to set the underlying source to an array of bytes.
    Perhaps you could use a stringstream, though I am not sure how well that would play with binary content.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Chris87 View Post
    There seems to be no way to set the underlying source to an array of bytes.
    Not quite sure what you're after, but, there is actually a clear way:

    Code:
    struct underlying_source
    {
        char muh_data[33];
        int moar_data;
    };
    
    auto p = static_cast<const underlying_source*>(data + 9); // start at byte 10, it's how i roll, dawg
    user->SetName(p->muh_data);

  4. #4
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139
    Hmm... I reordered the fields to reduce memory footprint, however.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Chris87 View Post
    Hmm... I reordered the fields to reduce memory footprint, however.
    I don't know what you mean by that.
    Regardless, I would think Yarin's idea is a good one. std::string also has a constructor that takes two iterators for [begin, end), allowing you to construct a string from a sequence of bytes.
    Otherwise I'd propose a helper function/object, something along the lines of (won't work for strings, though):

    Code:
    template<typename T>
    T func(int& pos, unsigned char* data)
    {
        auto newdata = *reinterpret_cast<T*>(&data[pos]);
        pos += sizeof(T);
        return newdata;
    }
    stringstreams may also work (as laserlight mentions(. I haven't tested them with raw data, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139
    stringstream does seem to work for me. I just tried it out. I did do a test with it some time ago. I guess I either remember it wrong or did something silly like stream << &someInt;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-22-2016, 03:20 AM
  2. reading files byte by byte
    By cpsc in forum C++ Programming
    Replies: 12
    Last Post: 01-07-2011, 03:54 PM
  3. Replies: 2
    Last Post: 01-30-2009, 05:58 AM
  4. Converting byte arrays to vectors
    By kasun in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2004, 10:31 AM
  5. byte arrays & vectors
    By kasun in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 09:10 AM