std::cout << stringstream.str()->c_str() 不打印任何内容

发布于 2024-10-06 02:29:32 字数 1349 浏览 5 评论 0 原文

在一个函数中,得到 unsigned char && unsigned char length,

void pcap_callback(u_char *args, const struct pcap_pkthdr* pkthdr, const u_char* packet) 
{
    std::vector<unsigned char> vec(packet, packet+pkthdr->len); // optimized from foo.
    std::stringstream scp;
    for (int i=0;i<pkthdr->len;i++) {
        scp<<vec[i];
    }
    std::string mystr =  std::string(scp.rdbuf()->str());
    std::cout << "WAS: " << packet << std::endl;
    std::cout << "GOOD: " << scp.str() << std::endl;
    std::cout << "BAD: "  << scp.str().c_str() << std::endl;
    std::cout << "TEST: " << mystr.size() << std::endl;
    assert(mystr.size() == pkthdr->len); 
}

结果:

  • WAS:不打印任何内容(猜测有一个指向 const..case 的指针)
  • 好:打印数据
  • 坏:不打印任何
  • 内容 TEST,断言:打印 mystr.size() 等于传递的 unsigned char 大小。

我尝试过:

  • string.assign(scp.rdbuf());
  • memcpy(char, scp.str(), 10);
  • 创建/分配临时字符、字符串的不同方法

没有帮助..希望获得一个包含数据的std::cout'ablestd::string,(其中是从 foo 中选取的,它是 unsigned char,它是数据包数据)。

猜测原始的 foo 可能不是空终止的,或者问题是这样的 - 简单,但无法进入..这里要寻找什么?

(此代码是使用 libpcap 的另一次尝试,只是以 C++ 方式打印数据包,而不使用已知的 C++ 魔术包装器(如 libpcapp)。

in a function, that gets unsigned char && unsigned char length,

void pcap_callback(u_char *args, const struct pcap_pkthdr* pkthdr, const u_char* packet) 
{
    std::vector<unsigned char> vec(packet, packet+pkthdr->len); // optimized from foo.
    std::stringstream scp;
    for (int i=0;i<pkthdr->len;i++) {
        scp<<vec[i];
    }
    std::string mystr =  std::string(scp.rdbuf()->str());
    std::cout << "WAS: " << packet << std::endl;
    std::cout << "GOOD: " << scp.str() << std::endl;
    std::cout << "BAD: "  << scp.str().c_str() << std::endl;
    std::cout << "TEST: " << mystr.size() << std::endl;
    assert(mystr.size() == pkthdr->len); 
}

Results:

  • WAS: prints nothing (guess there is a pointer to const.. case)
  • GOOD: prints data
  • BAD: prints nothing
  • TEST, assert: prints that mystr.size() is equal to passed unsigned char size.

I tried:

  • string.assign(scp.rdbuf());
  • memcpy(char, scp.str(), 10);
  • different methods of creating/allocating temporary chars, strings

No help.. it is wanted to get a std::cout'able std::string that contains data, (which was picked from foo, which was unsigned char, which was packet data).

Guessing either the original foo may not be null-terminated, or the problem is something like this - simple, but can't get in.. what are the things to look for here?

(this code is another attempt to use libpcap, just to print packets in C++ way, without using known C++ magic wrappers like libpcapp).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朦胧时间 2024-10-13 02:29:32

为了进行快速测试,请检查 scp.str().size() == strlen(scp.str().c_str()) 以查看是否嵌入了“\0”字符串中的字符,我怀疑正在发生这种情况。

For a quick test, throw in a check for scp.str().size() == strlen(scp.str().c_str()) to see if there are embedded '\0' characters in the string, which is what I suspect is happening.

给不了的爱 2024-10-13 02:29:32

我认为你处理这个问题的方式是错误的。看起来您正在此处处理二进制数据,在这种情况下,您不能期望将其作为文本有意义地输出到屏幕。您真正需要的是一个十六进制转储

const unsigned char* ucopy = packet;
std::ios_base::fmtflags old_flags = std::cout.flags();
std::cout.setf(std::ios::hex, std::ios::basefield);

for (const unsigned char* p = ucopy, *e = p + pkthdr->len; p != e; ++p) {
    std::cout << std::setw(2) << std::setfill('0') << static_cast<unsigned>(*p) << " ";
}

std::cout.flags(old_flags);

这将逐字节输出数据,并让您检查二进制数据的各个十六进制值。空字节将简单地输出为 00

I think you're going about this the wrong way. It looks like you're dealing with binary data here, in which case you can't expect to meaningfully output it to the screen as text. What you really need is a hex dump.

const unsigned char* ucopy = packet;
std::ios_base::fmtflags old_flags = std::cout.flags();
std::cout.setf(std::ios::hex, std::ios::basefield);

for (const unsigned char* p = ucopy, *e = p + pkthdr->len; p != e; ++p) {
    std::cout << std::setw(2) << std::setfill('0') << static_cast<unsigned>(*p) << " ";
}

std::cout.flags(old_flags);

This will output the data byte-by-byte, and let you examine the individual hex values of the binary data. A null byte will simply be output as 00.

怪我鬧 2024-10-13 02:29:32

输出尝试失败后检查 std::cout.good()。我的猜测是输出出现一些故障(即尝试将不可打印的字符写入控制台),这是在 cout 上设置 failbit

还要检查以确保字符串不以 NULL 开头,否则会导致空输出成为预期行为:)

(旁注,请使用 reinterpret_cast 来表示 unsigned char *ucopy = (unsigned char*)packet; 如果您使用 C++ ;) )

Check std::cout.good() after the failed output attempt. My guess is that there's some failure on output (i.e. trying to write a nonprintable character to the console), which is setting failbit on cout.

Also check to ensure the string does not start with a NULL, which would cause empty output to be the expected behavior :)

(Side note, please use reinterpret_cast for unsigned char *ucopy = (unsigned char*)packet; if you're in C++ ;) )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文