Cout不打印数字

发布于 2024-12-14 11:49:01 字数 1070 浏览 6 评论 0原文

问题

我没有从简单的 cout 中得到任何输出,而 printf 总是会打印数字:

std::cout << variableuint8;  // prints nothing
printf("%u", variableuint8); // prints the number

我以前从未遇到过这种行为,虽然我有解决办法,但我想理解它。

上下文:

是的,指点。因此,它可能比所说的要复杂一些。这是上下文 - 我认为这并不重要,当 cout 和 printf 获取信息时,它已取消引用为简单的无符号字符。这可能比解决问题所需的信息要多,但我希望在相关的情况下能够完成。

typedef unsigned char   UInt8;
typedef unsigned short  UInt16;

typedef struct{
   UInt8   len;
   // some other definitions. sizeof(DeviceNotification) <= 8
}DeviceNotification;

UInt8 somerandomdata[8];
DeviceNotification * notification;

notification = (DeviceNotification *) somerandomdata;

std::cout << "Len: (" << notification->len << ")\n";
printf("Len: (%u)\n", notification->len);

随机数据在其他地方初始化。对于此输出,数组中的第一个字节是 0x08:

输出:

Len: ()
Len: (8)

环境

  • 开发机器:
    • OS X 10.6.8
    • 编译器 LLVM 1.7
    • Xcode 3.2.6
  • 测试机:
    • OS X 10.6.8
    • 操作系统

Issue

I'm getting no output from a simple cout, whereas a printf will always print the number:

std::cout << variableuint8;  // prints nothing
printf("%u", variableuint8); // prints the number

I've never run into this behavior before, and while I have a work around I would like to understand it.

Context:

Yay, pointers. So it may be a little more involved than as stated. Here's the context - I don't think it should matter, by the time cout and printf get the info it's dereferenced to a simple unsigned char. This is probably more information than needed to resolve the issue, but I wanted to be complete on the off chance that it was relevant.

typedef unsigned char   UInt8;
typedef unsigned short  UInt16;

typedef struct{
   UInt8   len;
   // some other definitions. sizeof(DeviceNotification) <= 8
}DeviceNotification;

UInt8 somerandomdata[8];
DeviceNotification * notification;

notification = (DeviceNotification *) somerandomdata;

std::cout << "Len: (" << notification->len << ")\n";
printf("Len: (%u)\n", notification->len);

The random data is initialized elsewhere. For this output, the first byte in the array is 0x08:

Output:

Len: ()
Len: (8)

Environment

  • Development machine:
    • OS X 10.6.8
    • Compiler LLVM 1.7
    • Xcode 3.2.6
  • Test machine:
    • OS X 10.6.8

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

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

发布评论

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

评论(3

你在看孤独的风景 2024-12-21 11:49:01

它将 char 作为字符打印。

#include <iostream>

int main() {
        unsigned char c = 0x41;
        std::cout << c << std::endl;
        return 0;
}

这会将“A”打印到标准输出。

尽管在这种情况下,您的代码应该打印 Len: (*),而且我确实已验证它会打印 Len: (*)


编辑:由于您的控制台可能使用 ASCII 或 UTF-8 等字符编码,因此与 8(退格键)对应的字符不可见,因此看起来好像没有打印出来。

(在某些情况下,可能会导致前一个字符(() 消失,因为它是退格字符。在 DOS 中可能会显示 ◘)

It's printing the char as a character.

#include <iostream>

int main() {
        unsigned char c = 0x41;
        std::cout << c << std::endl;
        return 0;
}

This prints 'A' to the stdout.

Although in that case, your code should print Len: (*), and I did have verified it prints Len: (*).


Edit: Since in character encodings like ASCII or UTF-8 your console is likely using, the character corresponding to 8 (Backspace) is not visible, it will look like it's not printed.

(In some cases it may cause the previous character (the () to disappear because it is a backspace character. In DOS it may show ◘)

潇烟暮雨 2024-12-21 11:49:01

您是否尝试将其转换为 int,以避免依赖于实现所使用的 char 类型:

std::cout << (int)variableuint8;

这是对所发生情况的解释:什么是 unsigned char?

为了获得一个想法,您的实现使用 unsigned char 作为字符。 0x08 的 ASCII 代码是退格控制字符,当然它不是可打印字符,这就是为什么在 std::cout 的情况下看不到输出。

Have you tried to cast it into int, to avoid depending on the char type that is used by the implementation:

std::cout << (int)variableuint8;

Here is an explanation of what is going on: What is an unsigned char?

To get an idea, your implementation is using unsigned char as char. The ASCII code for 0x08 is the backspace control character, and of course it is not a printable character, that is why you don't see an output in the case of std::cout.

仙女山的月亮 2024-12-21 11:49:01

std::cout << "Len: (" << (unsigned Short)notification->len << ")\n"; 打印正确的值?我猜想转换为整数可能就是我们想要的。

Would std::cout << "Len: (" << (unsigned short)notification->len << ")\n"; print the correct value? I guess casting to an integer is what might be looking for.

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