Hi I need help on converting the sample c++ code. Can someone please help. Any help would be greatly appreciated. :D

struct sample
{
int type;
long arg1, arg2;
};

struct sample sampletable[2][3] =
{{{ 1, 0, 0 },
{ 2, 0, 0 },
{ 1, 0, 0 }},
{{ 2, 0, 0 },
{ 3, 0, 0 },
{ 4, 0, 0 }},
{{ 5, 0, 0 },
{ 6, 0, 0 },
{ 1, 0, 0 }}}

Hi,

Try this :

type sample = record
_type,arg1,arg2 : integer; 
{ type is a reserved word in Delphi, and integer type is 32 bit wide; for 16 bit unsigned you can use Word or for 8 bit unsigned you can use byte; there are also shortint and other integer types, you can check the help
}
end;

var sampletable : Array [0..1,0..2] of sample = ( ((_type:1;arg1:1;arg2:1),(_type:1;arg1:1;arg2:1),(_type:1;arg1:1;arg2:1)) , ((_type:1;arg1:1;arg2:1),(_type:1;arg1:1;arg2:1),(_type:1;arg1:1;arg2:1)) );

Loren Soth

Hi for all,
I'm newer with Delphi can anybody help me to convert the following c++ code to
delphi7 one's.

many thanks in advance

c++ code :

********************************* 
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#define BUFSIZE  216  /* size of message buffer */

void usage(char *progname)
{
    printf("usage: %s [-h host] [-p port]\n", progname);
    exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
    char               *host = "localhost";
    int                 port = 6661;
    int                 fd;
    struct hostent     *hostentry = NULL;
    struct sockaddr_in  addr;
    int                 n;
    int                 c, errflg = 0;
    char                buffer[BUFSIZE];
    char  header[7];
    char  ibuffer[256];
    FILE *f = freopen("/home/err_log","w",stderr); 

   memset(ibuffer, 0, 256);
   memset ( header, 0, sizeof ( header ));
   header[0] = ((strlen(argv[5])+4)/256);
   header[1] = ((strlen(argv[5])+4)%256);
   memcpy ( header + 2 , "1234", 4); 
   memcpy ( ibuffer ,header , 6);
   memcpy ( ibuffer + 6 ,argv[5] ,strlen(argv[5]));
    while ((c=getopt(argc, argv, "h:p:")) != -1) {
        switch(c) {
    case 'h':   /* server host name */
        host = optarg;
        break;
        case 'p':   /* server port */
            port = atoi(optarg);
            break;
        default:    /* the rest are errors */
            errflg++;
            break;
        }
    }

    if (host == NULL || port == 0 || errflg) {
        usage(argv[0]);
    }

    signal(SIGPIPE, SIG_IGN);

    /* create a TCP socket */
    if ((fd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    printf("sock00:socket error %d\n", errno);
        exit(EXIT_FAILURE);           
    }


    /* connect to the socket server */
    memset(&addr, 0, sizeof(struct sockaddr_in));
    addr.sin_family = AF_INET;
    addr.sin_port   = htons(port);

    if (strcmp(host, "any") == 0) {
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    } else if (strcmp(host, "loopback") == 0) {
    addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    } else {
    if ((hostentry = gethostbyname(host)) == NULL) {
        printf("sock00:invalid hostname \"%s\"\n", host);
        exit(EXIT_FAILURE);
    }
    memcpy(&addr.sin_addr.s_addr, hostentry->h_addr, hostentry->h_length);
    }                      



    if (connect(fd, (struct sockaddr *)&addr,
        sizeof(struct sockaddr)) < 0) {
    printf("sock00:connect error %d\n", errno);
        exit(EXIT_FAILURE);
    }

    if (send(fd, ibuffer, sizeof(ibuffer), 0) <= 0) {
    printf("sock00:send error %d\n", errno);
    exit(EXIT_FAILURE);
    }

     memset(buffer, 0, sizeof(buffer));
       /* fprintf(stderr,"\nCMD   :%s\n",ibuffer+6); */
        if ((n = recv(fd, buffer, sizeof(buffer), MSG_WAITALL)) < 0) {
        printf("recv error %d\n", errno);
        exit(EXIT_FAILURE);
    }
/*    printf("%s\n",buffer+6 )  ;*/
      fprintf(stderr,"%s\n",buffer+10);
    close(fd);

    return EXIT_SUCCESS;
}

*********************************

Edited 3 Years Ago by Dani: Formatting fixed

I will be very grateful if someone could converse this program from C to Delphi.

unsigned short crc (unsigned char *p,int buflen, unsigned short start_crc)
{
  unsigned short temp;
  int i,j;
  unsigned char c;
  temp = start_crc;
  for (i=0; i<buflen; i++) {
    c = *p++;
    for (j=1; j<=8; j++) {
      if (temp & 1)  {
        temp ^= 0x8005;
        c ^= 1;
      }
      temp = temp >> 1;
      if (c & 1) temp l= 0x8000;
      c = c>> 1;
    }
  } 
  Return(~temp); 
 }

Edited 3 Years Ago by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.