Bhavya Arora
Bhavya Arora

Reputation: 49

Not able to print the char array as thought

#include <fstream>
#include<iostream>
#include<cstring>
using namespace std;

class Address {
public:
    char addr[6];
    Address() {}
    Address(string address) {
        size_t pos = address.find(":");
        int id = stoi(address.substr(0, pos));
        short port = (short)stoi(address.substr(pos + 1, address.size()-pos-1));
        memcpy(addr, &id, sizeof(int));
        memcpy(&addr[4], &port, sizeof(short));
    }
};
enum MsgTypes{
    JOINREQ,
    JOINREPLY,
    DUMMYLASTMSGTYPE,
    HEARTBEAT
};

/**
 * STRUCT NAME: MessageHdr
 *
 * DESCRIPTION: Header and content of a message
 */
typedef struct MessageHdr {
    enum MsgTypes msgType;
}MessageHdr;

    typedef struct en_msg {
        // Number of bytes after the class
        int size;
        // Source node
        Address from;
        // Destination node
        Address to;
    }en_msg;

//class Testing{

void send(Address *myaddr, Address *toaddr, char *data, int size);
    int main()
    {
        MessageHdr *msg=new MessageHdr();
        size_t msgsize = sizeof(MessageHdr) + sizeof(Address) + sizeof(long) + 1;
        msg=(MessageHdr  *)malloc(msgsize*sizeof(char));
        int id=233;
        short  port =22;
        long heartbeat=1;
        msg=(MessageHdr  *)malloc(msgsize*sizeof(char));
        string s=to_string(id)+":"+to_string(port);
        string s1=to_string(id+1)+":"+to_string(port+1);
        cout<<s<<'\n';
        cout<<s1<<'\n';
        Address *addr= new Address(s);
        for (int i = 0; i < 6; i++)
            cout << addr->addr[i];

        Address *toaddr= new Address(s1);
        msg->msgType = JOINREQ;
        //cout<<(char *)msg->msgType;
        memcpy((char *)(msg+1), addr, sizeof(addr));
        memcpy((char *)(msg+1) + 1 + sizeof(addr), &heartbeat, sizeof(long));
        send(addr, toaddr, (char *)msg,  msgsize);
        return 0;
    }

        void send(Address *myaddr, Address *toaddr, char *data, int size) {
            cout<<"inside send"<<'\n';
        en_msg *em;
//static char temp[2048];

        em = (en_msg *)malloc(sizeof(en_msg) + size);
        em->size = size;

        memcpy(&(em->from), &(myaddr), sizeof(em->from));
        memcpy(&(em->to), &(toaddr), sizeof(em->from));
        memcpy(em + 1, data, size);
        cout<<(char *)(em+1);
    }

This is my program,in between I am trying to check the address what is being stored in my char array. but upon printing the array, it gives some strange output. two strange symbols after printing the value of s and s1. I am trying to store the id:port in the char array of the address class, but looks without success. Please help

The code I am referring to for printing is in the main function. Approx ten lines down the main function.

For say, my id is 233 and port is 22, The address is 233:22 I want to retrieve back 233:22 and print it. How do I do that here?

Thanks in advance :)

Upvotes: 0

Views: 202

Answers (2)

ichramm
ichramm

Reputation: 6642

Given the following code:

for (int i = 0; i <= 6; i++)
            cout << addr->addr[i];

And given Address's constructor:

size_t pos = address.find(":");
int id = stoi(address.substr(0, pos));
short port = (short)stoi(address.substr(pos + 1, address.size()-pos-1));
memcpy(addr, &id, sizeof(int));
memcpy(&addr[4], &port, sizeof(short));

It's clear that you are printing the bytes that conform a number

addr->addr is a char array which contains two integer variables, one having two bytes (int) and the other having 2 bytes (short).

So, if the number is , lets say, 436, you are printing:

0xB4 0x01 0x00 0x00 
<crazy char> SOH NULL  NULL

You must understand what are you printing, or what you want to print in order to print it properly.

Note: The most popular setup is assumed here, which means:

  • Little Endian arquitecture
  • 4-byte int
  • 2-byte short

Update

How to get address and port back:

int address;
unsigned short port;
memset(&address, addr->addr, 4);
memset(&port, addr->addr+4, 2);

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308186

The problem is in this line:

cout << addr->addr[i];

Since addr->addr is an array of char, each element will be printed as the character it represents. If you'd rather print the integer value of each, simply cast it to int first.

cout << static_cast<int>(addr->addr[i]); // or old-fashioned: (int)addr->addr[i];

Upvotes: 1

Related Questions