Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > another easy byte buffer question

Reply
Thread Tools

another easy byte buffer question

 
 
andy.dreistadt@gmail.com
Guest
Posts: n/a
 
      01-12-2005
Hi all,

I came across another problem that is probably pretty easy but, again,
due to my rusty-ness with C, I'm a little stumped.

I have a struct that looks like this:

/* Instrument Data structure */
struct instrument_info
{
int ID;
char serial[30];
char type[30];
char model[30];
char name[30];
char location[30];
char last_calibration[30];
char calibration_results[10];
};

and and array of these structs as follows:

/* Instrument Data structure array*/
struct instrument_info instrument_array[MAX_INSTRUMENTS];


My question is this:
My program is communicating through UDP packets to a remote Java
program. I need to take the char arrays from the struct above and
place them into a byte buffer so they may be transferred in the UDP
packet. I need the end buffer to look something like this:
"ID,serial,type,model,name,location,last_calilbrat ion,calibration_results;ID,serial,type...."
So that a comma is the attribute delimeter, while the semicolon is the
instrument delimeter.

In Java the answer would be something along the lines of:

String send_buf = "";
for(int i = 0; i<=num_inst; i++)
{
send_buf += instrument_array[i].ID + ",";
send_buf += instrument_array[i].serial + ",";
send_buf += instrument_array[i].type + ",";
send_buf += instrument_array[i].model + ",";
send_buf += instrument_array[i].name + ",";
send_buf += instrument_array[i].location + ",";
send_buf += instrument_array[i].last_calibration + ",";
send_buf += instrument_array[i].calibration_results + ";";
}
byte[] send_bytes = send_buf.getBytes();

Due to my lack of C knowledge, I was wondering if anyone out there
could help.

Thanks in advance,

Andy

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-12-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi all,
>
> I came across another problem that is probably pretty easy but, again,
> due to my rusty-ness with C, I'm a little stumped.
>
> I have a struct that looks like this:
>
> /* Instrument Data structure */
> struct instrument_info
> {
> int ID;
> char serial[30];
> char type[30];
> char model[30];
> char name[30];
> char location[30];
> char last_calibration[30];
> char calibration_results[10];
> };
>
> and and array of these structs as follows:
>
> /* Instrument Data structure array*/
> struct instrument_info instrument_array[MAX_INSTRUMENTS];
>
>
> My question is this:
> My program is communicating through UDP packets to a remote Java
> program. I need to take the char arrays from the struct above and
> place them into a byte buffer so they may be transferred in the UDP
> packet. I need the end buffer to look something like this:
>

"ID,serial,type,model,name,location,last_calilbrat ion,calibration_results;ID
,serial,type...."
> So that a comma is the attribute delimeter, while the semicolon is the
> instrument delimeter.
>
> In Java the answer would be something along the lines of:
>
> String send_buf = "";
> for(int i = 0; i<=num_inst; i++)
> {
> send_buf += instrument_array[i].ID + ",";
> send_buf += instrument_array[i].serial + ",";
> send_buf += instrument_array[i].type + ",";
> send_buf += instrument_array[i].model + ",";
> send_buf += instrument_array[i].name + ",";
> send_buf += instrument_array[i].location + ",";
> send_buf += instrument_array[i].last_calibration + ",";
> send_buf += instrument_array[i].calibration_results + ";";
> }
> byte[] send_bytes = send_buf.getBytes();
>
> Due to my lack of C knowledge, I was wondering if anyone out there
> could help.


For manipulation of 'C-style' strings and arrays of characters,
see the functions declared by standard header <cstring>
(or <string.h>), such as 'memcpy()', 'strcpy()', 'strcat()', etc.
Note that you must be very careful with these, addressing issues
such as ensuring that enough memory is provided for the 'targets'
of copy operations, supplying 'string terminators' (where applicable),
etc.

Another possiblity is to do all your string manipulations with
objects of type 'std::string' (which does provide 'intuitive'
operations such as '+' for concatenation, etc.), then create
your 'C-style' strings from those (see the 'std::string::c_str()'
member function.)

-Mike


 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      01-12-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> Hi all,
>
> I came across another problem that is probably pretty easy but, again,
> due to my rusty-ness with C, I'm a little stumped.


So why then are you asking in comp.lang.C++


[snip]
>
> In Java the answer would be something along the lines of:
>
> String send_buf = "";
> for(int i = 0; i<=num_inst; i++)
> {
> send_buf += instrument_array[i].ID + ",";
> send_buf += instrument_array[i].serial + ",";
> send_buf += instrument_array[i].type + ",";
> send_buf += instrument_array[i].model + ",";
> send_buf += instrument_array[i].name + ",";
> send_buf += instrument_array[i].location + ",";
> send_buf += instrument_array[i].last_calibration + ",";
> send_buf += instrument_array[i].calibration_results + ";";
> }
> byte[] send_bytes = send_buf.getBytes();
>
> Due to my lack of C knowledge, I was wondering if anyone out there
> could help.


Since all your struct members are already C-style strings a very similar
solution can be done in C++

#include <string>

....

std::string send_buf;

for( int i = 0; i < num_inst; ++i ) {
send_buf += instrument_array[i].ID;
send_buf += ",";
send_buf += instrument_array[i].serial;
send_buf += ",";

...

Just in case you really need a C solution:
eg.

char send_buf[some_very_large_number];

send_buf[0] = '\0';
for( int i = 0; i < num_inst; ++i ) {
strcat( send_buf, instrument_array[i].ID;
strcat( send_buf, "," );
strcat( send_buf, instrument_array[i].serial;
strcat( ....

or a different C solution

char send_buf[some_very_large_number];
char tmp_buf[some_very_large_number];

for( int i = 0; i < num_inst; ++i ) {
sprintf( tmp_buf, "%s,%s,%s;", instrument_array[i].ID,
instrument_array[i].serial,
instrument_array[i].type );
strcat( send_buf, tmp_buf );
}


Your C must be very rusty, all of this is basic string handling in either
C++ or C.

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
convert M bit buffer to N bit buffer runcyclexcski@yahoo.com C++ 2 03-26-2007 09:43 AM
How to extract x amount of bytes from a byte object and store into another byte obj. DaBeef Java 1 07-21-2006 05:20 PM
How to know the buffer size and increase buffer size in c++ Raja C++ 12 06-21-2004 06:21 PM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments