another easy byte buffer question

Discussion in 'C++' started by andy.dreistadt@gmail.com, Jan 12, 2005.

  1. Guest

    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
     
    , Jan 12, 2005
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Jan 12, 2005
    #2
    1. Advertising

  3. 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.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.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jan 12, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,406
    Roedy Green
    Aug 5, 2003
  2. Raja
    Replies:
    12
    Views:
    24,646
    John Harrison
    Jun 21, 2004
  3. DaBeef
    Replies:
    1
    Views:
    631
    Matt Humphrey
    Jul 21, 2006
  4. Replies:
    2
    Views:
    623
    sergejusz
    Mar 26, 2007
  5. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    423
    Neal Becker
    Jan 8, 2009
Loading...

Share This Page