473,405 Members | 2,445 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,405 software developers and data experts.

another easy byte buffer question

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

Jul 22 '05 #1
2 1765

<an************@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr 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
Jul 22 '05 #2
an************@gmail.com 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
kb******@gascad.at
Jul 22 '05 #3

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

Similar topics

8
by: Skwerl | last post by:
Hi guys. I've written code to embed an ICC profile in a TIFF image, and I think my IO operations are slowing things down. It is taking about a second to embed each tag in 7-meg TIFF files. ...
13
by: Ray Z | last post by:
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert...
14
by: Daniel Billingsley | last post by:
The example code for Memory shows it being used in a using() block. The documentation for using() says it can only be used on things that implement Disposable. Yet there is no Dispose() method...
5
by: Eric | last post by:
Is there a class that will allow me to append a byte to another? The data that I have is binary so StringBuilder will not work.
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
10
by: D. Yates | last post by:
All, Is there a faster way to do this in C#: byte buffer; buffer = new byte; for(int i = 0; i < buffer.Length; i++) buffer = 0;
2
by: 7elephants | last post by:
I have the following piece of code to take data from one stream and put it into another... Int32 bufferSize = 100; Int32.TryParse(ConfigurationManager.AppSettings.ToString(), out bufferSize);...
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.