Reading in and out of a character buffer

N

n_jaksic

I need to store heterogeneous data (for example, a string, a vector of
floats and some ints) in an unsigned character array, for the purpose
of storing information in some header. I then need to be able to read
this data back into their original types. I could store the size of the
vector and the string, as well as the number of ints along with the
data to help with the reading later on. However, I'm having a hard
time storing and then reading back correctly numeric types in a
character array. Simple casting doesn't seem to do the trick. Any
ideas? Thanks.
 
M

mlimber

I need to store heterogeneous data (for example, a string, a vector of
floats and some ints) in an unsigned character array, for the purpose
of storing information in some header. I then need to be able to read
this data back into their original types. I could store the size of the
vector and the string, as well as the number of ints along with the
data to help with the reading later on. However, I'm having a hard
time storing and then reading back correctly numeric types in a
character array. Simple casting doesn't seem to do the trick. Any
ideas? Thanks.

Yes. See these FAQs on serialization:

http://www.parashift.com/c++-faq-lite/serialization.html

Also consider Boost.Serialization:

http://boost.org/libs/serialization/doc/index.html

Cheers! --M
 
J

Jim Langston

I need to store heterogeneous data (for example, a string, a vector of
floats and some ints) in an unsigned character array, for the purpose
of storing information in some header. I then need to be able to read
this data back into their original types. I could store the size of the
vector and the string, as well as the number of ints along with the
data to help with the reading later on. However, I'm having a hard
time storing and then reading back correctly numeric types in a
character array. Simple casting doesn't seem to do the trick. Any
ideas? Thanks.

"Simple casting doesn't seem to do the trick." What do you mean by this? I
do this type of thing all the time for various things, usually sending data
over the network.

If I have an int and a character pointer, treat the character pointer as an
int pointer and derefence it.

(Untested code)

unsigned char MyArray[100];
int MyInt = 20;

char* CharP = &MyArray[10];

*reinterpret_cast<int*>( CharP ) = MyInt;
or
*reinterpret_cast<int*>( &MyArray[10] ) = MyInt;

This would change bytes 10, 11, 12 and 13 of the char array to the binary
value located in MyInt. To load it back go the other way.

MyInt = *reinterpret_cast<int*>( &MyArray[10] );

If this gives you problems (it should work fine as is?) you may need an
extra set of parenthesis:

MyInt = *reinterpret_cast<int*>( &(MyArray[10]) );

but I don't think you do.
 
N

n_jaksic

Great, thanks a lot!


Jim said:
I need to store heterogeneous data (for example, a string, a vector of
floats and some ints) in an unsigned character array, for the purpose
of storing information in some header. I then need to be able to read
this data back into their original types. I could store the size of the
vector and the string, as well as the number of ints along with the
data to help with the reading later on. However, I'm having a hard
time storing and then reading back correctly numeric types in a
character array. Simple casting doesn't seem to do the trick. Any
ideas? Thanks.

"Simple casting doesn't seem to do the trick." What do you mean by this? I
do this type of thing all the time for various things, usually sending data
over the network.

If I have an int and a character pointer, treat the character pointer as an
int pointer and derefence it.

(Untested code)

unsigned char MyArray[100];
int MyInt = 20;

char* CharP = &MyArray[10];

*reinterpret_cast<int*>( CharP ) = MyInt;
or
*reinterpret_cast<int*>( &MyArray[10] ) = MyInt;

This would change bytes 10, 11, 12 and 13 of the char array to the binary
value located in MyInt. To load it back go the other way.

MyInt = *reinterpret_cast<int*>( &MyArray[10] );

If this gives you problems (it should work fine as is?) you may need an
extra set of parenthesis:

MyInt = *reinterpret_cast<int*>( &(MyArray[10]) );

but I don't think you do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top