无法像想象的那样打印char数组

时间:2015-03-20 17:31:01

标签: c++ arrays c++11

#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);
    }

这是我的程序,在我之间我试图检查地址存储在我的char数组中。但是在打印阵列时,它会产生一些奇怪的输出。打印s和s1的值后出现两个奇怪的符号。  我试图将id:port存储在地址类的char数组中,但看起来没有成功。请帮忙

我所指的打印代码是主要功能。主函数大约十行。

比如说,我的身份证是233,端口是22,地址是233:22我想要回溯233:22并打印出来。我怎么在这里做到这一点?

提前致谢:)

2 个答案:

答案 0 :(得分:1)

问题出在这一行:

cout << addr->addr[i];

由于addr->addrchar的数组,因此每个元素都将打印为它所代表的字符。如果您更愿意打印每个的整数值,只需先将其转换为int

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

答案 1 :(得分:0)

给出以下代码:

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

给定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));

很明显,您正在打印符合数字的字节

addr->addr是一个char数组,它包含两个整数变量,一个有两个字节(int),另一个有2个字节(short)。

所以,如果数字是436,那么你正在打印:

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

您必须了解要打印的内容或要打印的内容才能正确打印。

注意:这里假设最流行的设置,这意味着:

  • Little Endian arquitecture
  • 4-byte int
  • 2字节短

<强>更新

如何获取地址和端口:

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