简体   繁体   English

无法按预期打印char数组

[英]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. 这是我的程序,在这段时间里,我试图检查该地址在char数组中存储了什么。 but upon printing the array, it gives some strange output. 但是在打印数组时,它会给出一些奇怪的输出。 two strange symbols after printing the value of s and s1. 在打印出s和s1的值后,出现两个奇怪的符号。 I am trying to store the id:port in the char array of the address class, but looks without success. 我正在尝试将id:port存储在地址类的char数组中,但看起来没有成功。 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. 例如,我的ID是233,端口是22,地址是233:22,我想找回233:22并打印出来。 How do I do that here? 我该怎么办?

Thanks in advance :) 提前致谢 :)

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. 由于addr->addrchar数组,因此每个元素都将addr->addr表示的字符进行打印。 If you'd rather print the integer value of each, simply cast it to int first. 如果要打印每个整数值,只需将其首先转换为int

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

Given the following code: 给出以下代码:

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

And given Address 's constructor: 并给定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));

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). addr->addr是一个char数组,包含两个整数变量,一个具有两个字节(int),另一个具有2个字节(short)。

So, if the number is , lets say, 436, you are printing: 因此,如果数字为,假设是436,则您正在打印:

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 4字节整数
  • 2-byte short 2字节短

Update 更新

How to get address and port back: 如何获取地址和端口:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM