another easy byte buffer question

A

andy.dreistadt

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_calilbration,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.ID + ",";
send_buf += instrument_array.serial + ",";
send_buf += instrument_array.type + ",";
send_buf += instrument_array.model + ",";
send_buf += instrument_array.name + ",";
send_buf += instrument_array.location + ",";
send_buf += instrument_array.last_calibration + ",";
send_buf += instrument_array.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
 
M

Mike Wahler

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_calilbration,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.ID + ",";
send_buf += instrument_array.serial + ",";
send_buf += instrument_array.type + ",";
send_buf += instrument_array.model + ",";
send_buf += instrument_array.name + ",";
send_buf += instrument_array.location + ",";
send_buf += instrument_array.last_calibration + ",";
send_buf += instrument_array.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
 
K

Karl Heinz Buchegger

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.ID + ",";
send_buf += instrument_array.serial + ",";
send_buf += instrument_array.type + ",";
send_buf += instrument_array.model + ",";
send_buf += instrument_array.name + ",";
send_buf += instrument_array.location + ",";
send_buf += instrument_array.last_calibration + ",";
send_buf += instrument_array.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.ID;
send_buf += ",";
send_buf += instrument_array.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.ID;
strcat( send_buf, "," );
strcat( send_buf, instrument_array.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.ID,
instrument_array.serial,
instrument_array.type );
strcat( send_buf, tmp_buf );
}


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top