Connecting Tech Pros Worldwide Help | Site Map

Reading in and out of a character buffer

  #1  
Old September 5th, 2006, 04:05 PM
n_jaksic@hotmail.com
Guest
 
Posts: n/a
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.

  #2  
Old September 5th, 2006, 04:15 PM
mlimber
Guest
 
Posts: n/a

re: Reading in and out of a character buffer


n_jaksic@hotmail.com wrote:
Quote:
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-lit...alization.html

Also consider Boost.Serialization:

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

Cheers! --M

  #3  
Old September 5th, 2006, 07:05 PM
Jim Langston
Guest
 
Posts: n/a

re: Reading in and out of a character buffer


<n_jaksic@hotmail.comwrote in message
news:1157469102.844862.41350@h48g2000cwc.googlegro ups.com...
Quote:
>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.


  #4  
Old September 6th, 2006, 07:35 PM
n_jaksic@hotmail.com
Guest
 
Posts: n/a

re: Reading in and out of a character buffer


Great, thanks a lot!


Jim Langston wrote:
Quote:
<n_jaksic@hotmail.comwrote in message
news:1157469102.844862.41350@h48g2000cwc.googlegro ups.com...
Quote:
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.
  #5  
Old September 6th, 2006, 08:55 PM
Default User
Guest
 
Posts: n/a

re: Reading in and out of a character buffer


n_jaksic@hotmail.com wrote:
Quote:
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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Parse a File in C AdrianH insights 0 June 4th, 2007 06:02 PM
reading file backwards and parsing Matt DeFoor answers 11 November 14th, 2005 05:43 AM
reading file backwards and parsing Matt DeFoor answers 11 November 14th, 2005 05:31 AM
How to read string out of file with pointer? Xinyi Yang answers 10 November 13th, 2005 07:15 PM