473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Placing bytes into a stream

I have a pointer to a buffer of bytes that I receive from a tcp stream that
I would like to place into some kind of stream that can I can then use to
serialize the data into into individual variables. I looked at the
streambuf, istream, and strstream, and can't quite figure out a simple
implementation for what I need.

Given a pointer to a buffer of bytes, and the size of the bytes that I am
concerned with, how can I get this into a stream that I can then use to
serialize the data to individual variables? The data is organized such that
there are two long values (4 bytes each), followed by two integers, then a
20 byte string of characters, etc...

jonathan
Jul 19 '05 #1
5 5991

"Jonathan Halterman" <ni*@aol.com> wrote in message
news:vo************@corp.supernews.com...
I have a pointer to a buffer of bytes that I receive from a tcp stream that I would like to place into some kind of stream that can I can then use to
serialize the data into into individual variables.
This is *de*serialization.
I looked at the
streambuf, istream, and strstream, and can't quite figure out a simple
implementation for what I need.

Given a pointer to a buffer of bytes, and the size of the bytes that I am
concerned with, how can I get this into a stream that I can then use to
serialize the data to individual variables? The data is organized such that there are two long values (4 bytes each), followed by two integers, then a
20 byte string of characters, etc...

jonathan


By definition, the size of a char is one byte in C++.
If the number of bits per char in the buffer don't
match your implementation's type 'char' number of bits,
you'll need to do a conversion.

I'm assuming your description above is indicating that
your implementation's type 'long' is actually 4 bytes
in size, and that the 'int' types in the buffer also
match the size for your implementation.

You may also need to 'fix up' the byte ordering
(e.g. 'big-endian', 'little-endian')

#include <algorithm>
#include <iostream>

void foo(const char* buffer)
{
const long *pl(reinterpret_cast<long*>(buffer));
long L1 = *pl++;
long L2 = *pl++;

const int *pi(reinterpret_cast<int*>(pl);
int I1 = *pi++;
int I2 = *pi++;

const char *p = reinterpret_cast<char *>(pi);
char array[21] = {0}
std:: copy(p, p + 20, array);

std::cout << L1 << '\n'
<< L2 << '\n'
<< I1 << '\n'
<< I2 << '\n'
<< array << '\n';
}

Not tested.

-Mike
Jul 19 '05 #2
Thanks for the example Mike. I will give this "deserialization" method a
try.

jonathan

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:nF*****************@newsread3.news.pas.earthl ink.net...

"Jonathan Halterman" <ni*@aol.com> wrote in message
news:vo************@corp.supernews.com...
I have a pointer to a buffer of bytes that I receive from a tcp stream

that
I would like to place into some kind of stream that can I can then use to serialize the data into into individual variables.


This is *de*serialization.
I looked at the
streambuf, istream, and strstream, and can't quite figure out a simple
implementation for what I need.

Given a pointer to a buffer of bytes, and the size of the bytes that I am concerned with, how can I get this into a stream that I can then use to
serialize the data to individual variables? The data is organized such

that
there are two long values (4 bytes each), followed by two integers, then a 20 byte string of characters, etc...

jonathan


By definition, the size of a char is one byte in C++.
If the number of bits per char in the buffer don't
match your implementation's type 'char' number of bits,
you'll need to do a conversion.

I'm assuming your description above is indicating that
your implementation's type 'long' is actually 4 bytes
in size, and that the 'int' types in the buffer also
match the size for your implementation.

You may also need to 'fix up' the byte ordering
(e.g. 'big-endian', 'little-endian')

#include <algorithm>
#include <iostream>

void foo(const char* buffer)
{
const long *pl(reinterpret_cast<long*>(buffer));
long L1 = *pl++;
long L2 = *pl++;

const int *pi(reinterpret_cast<int*>(pl);
int I1 = *pi++;
int I2 = *pi++;

const char *p = reinterpret_cast<char *>(pi);
char array[21] = {0}
std:: copy(p, p + 20, array);

std::cout << L1 << '\n'
<< L2 << '\n'
<< I1 << '\n'
<< I2 << '\n'
<< array << '\n';
}

Not tested.

-Mike

Jul 19 '05 #3
>>> I have a pointer to a buffer of bytes that I receive from a tcp
stream that I would like to place into some kind of stream that can
I can then use to serialize the data into into individual variables.


This is *de*serialization.

Thanks for the example Mike. I will give this "deserialization"
method a try.

Please do not top-post.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.4
Jonathan
Jul 19 '05 #4
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:nF*****************@newsread3.news.pas.earthl ink.net...

void foo(const char* buffer)
{
const long *pl(reinterpret_cast<long*>(buffer));
const long *pl = reinterpret_cast<long*>(buffer);
long L1 = *pl++;
long L2 = *pl++;

const int *pi(reinterpret_cast<int*>(pl);
const int *pi = reinterpret_cast<int*>(pl);
Not tested.


Or compiled. :-)

-Mike

Jul 19 '05 #5

"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:CJ*******************@weber.videotron.net...
I have a pointer to a buffer of bytes that I receive from a tcp
stream that I would like to place into some kind of stream that can
I can then use to serialize the data into into individual variables.

This is *de*serialization.

Thanks for the example Mike. I will give this "deserialization"
method a try.

Please do not top-post.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.4


Bet you were waiting all week to say that to someone. Well done.

jonathan
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rakesh Sinha | last post by:
Hi, I am writing this application in C++. It reads data from binary files. My current requirement is that: Given a positive number N, I have to read in N bytes from the input stream (which is...
2
by: Jeroen Ceuppens | last post by:
I created new bitmap file, bitmapdata, now a want the pixeldata that are in the array bytes put into the bmpData, what should I change in the code unsafe? there is something wrong with p =...
2
by: SK | last post by:
Hi, I am not able to convert the response stream from HttpWebResponse into bytes properly. Here is the relevent code - HttpWebResponse response = (HttpWebResponse)request.GetResponse (); ...
4
by: martin | last post by:
Hi, I am copying an xml file like so. Dim xmlDoc As New XmlDocument xmlDoc.Load("C:\Program Files\Templates\message.msg") Console.WriteLine("Tmaplate loaded") xmlDoc.Save("C:\Program...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
8
by: paraidy | last post by:
Hi all, as from object i need to read all byte from a file example c: \myphoto.jpg and recreate the file with another name to another directory c:\photo\recreatedphoto.jpg can someone write a...
2
by: Mike P2 | last post by:
I made a Stream-inheriting class that just removes the tabs (actually the 4 spaces VS prefers to use) from the beginning of lines and empty lines. At first I was having trouble with it adding a...
9
by: coconet | last post by:
I am trying to use a StreamReader to read consecutive bytes into a byte array until a ";" is hit, then store everything read up until that point into a new byte array. I have a...
4
by: Rymfax | last post by:
Greetings, I have an app that uses HttpWebRequest to download a rather large file (100 MB). I have a while loop set up so that it reads 4096 bytes at a time from the ResponseStream() until it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.