Connecting Tech Pros Worldwide Help | Site Map

Reading in and out of a character buffer

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 5th, 2006, 03:05 PM
n_jaksic@hotmail.com
Guest
 
Posts: n/a
Default Reading in and out of a character buffer

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, 03:15 PM
mlimber
Guest
 
Posts: n/a
Default 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, 06:05 PM
Jim Langston
Guest
 
Posts: n/a
Default 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, 06:35 PM
n_jaksic@hotmail.com
Guest
 
Posts: n/a
Default 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, 07:55 PM
Default User
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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 220,840 network members.