sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
n_jaksic@hotmail.com's Avatar

Reading in and out of a character buffer


Question posted by: n_jaksic@hotmail.com (Guest) on September 5th, 2006 04:05 PM
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.

4 Answers Posted
mlimber's Avatar
Guest - n/a Posts
#2: Re: Reading in and out of a character buffer

Join Bytes! wrote:
Quote:
Originally Posted by
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-li...ialization.html

Also consider Boost.Serialization:

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

Cheers! --M

Jim Langston's Avatar
Guest - n/a Posts
#3: Re: Reading in and out of a character buffer

<n_jaksic@hotmail.comwrote in message
news:1157469102.844862.41350@h48g2000cwc.googlegro ups.com...
Quote:
Originally Posted by
>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_jaksic@hotmail.com's Avatar
n_jaksic@hotmail.com September 6th, 2006 07:35 PM
Guest - n/a Posts
#4: Re: Reading in and out of a character buffer

Great, thanks a lot!


Jim Langston wrote:
Quote:
Originally Posted by
<n_jaksic@hotmail.comwrote in message
news:1157469102.844862.41350@h48g2000cwc.googlegro ups.com...
Quote:
Originally Posted by
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.


Default User's Avatar
Guest - n/a Posts
#5: Re: Reading in and out of a character buffer

Join Bytes! wrote:
Quote:
Originally Posted by
Great, thanks a lot!



Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>



Brian
 
Not the answer you were looking for? Post your question . . .
197,013 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,013 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors