Search code examples
c++stdstdoutomnet++

About converting the received message from ASCII code to human-readable message in OMNeT++


I sent a 'hello, world' message to a node in OMNeT++through the interface of ext module. I received {104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100} in OMNeT++. I found that this is its ASCII code, so I wrote a conversion program, but this program can convert the ASCII code into a string character string in the dev c++compiler. However, its application in OMNeT++has no effect. The following is the code of the conversion program.

I hope you can help me find out the key to the problem and give suggestions to solve it. Thanks again!!!

EV<<"pos="<<pos<<endl;
 std::string new_s = str.substr(pos); // 提取从pos位置到末尾的子串
 char myArray[new_s.size()+1];
 strncpy(myArray, new_s.c_str(),new_s.size()+1);
 myArray[new_s.size()-1]='\0';
std::stringstream test;
     for (int i = 0; i <sizeof(myArray) ; i++) {
        test << myArray[i];
     }
     // 将字符串流转换为字符串类型
std:: string want = test.str();
std::cout << "Received message: " << want << std::endl;
std::string str1(myArray);
 // 输出结果
 EV << str1 << endl;

Solution

  • According to what you have presented str is a std::string object that contains "hello, world". So in order to print it, no conversion is required.

    To show it in the simulation window:

    EV << "1 Received message: " << str << std::endl;
    

    Result:
    enter image description here

    To show it in the console:

    std::cout << "2 Received message: " << str << std::endl;
    

    Result:
    enter image description here