C++ cout 不打印无符号字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15585267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:30:47  来源:igfitidea点击:

cout not printing unsigned char

c++printf

提问by Hitesh Menghani

I am working on below code:

我正在处理以下代码:

#include<iostream>
#include<stdio.h>

using namespace std;

main() {
    unsigned char a;
    a=1;
    printf("%d", a);
    cout<<a;
}

It is printing 1 and some garbage.

它正在打印 1 和一些垃圾。

Why coutis behaving so?

为什么cout行为如此?

回答by Nawaz

cout << ais printing a value which appears to be garbage to you. It is not garbage actually. It is just a non-printableASCII character which is getting printed anyway. Note that ASCII character corresponding to 1is non-printable. You can check whether ais printable or not using, std::isprintas:

cout << a正在打印一个对您来说似乎是垃圾的值。其实不是垃圾。它只是一个不可打印的ASCII 字符,无论如何都会被打印出来。请注意,对应的 ASCII 字符1是不可打印的。您可以检查是否a可打印使用,std::isprint如:

std::cout << std::isprint(a) << std::endl;

It will print 0(read: false) indicating the character is non-printable

它将打印0(读取false:)指示字符是不可打印的

--

——

Anyway, if you want your coutto print 1also, then cast ato this:

无论如何,如果您还想cout打印1,请转换a为:

cout << static_cast<unsigned>(a) << std::endl;

回答by Francis Cugler

I had a similar issue herethat I've long forgotten about. The resolution to this problem with iostream'scoutcan be done like this:

我在这里遇到了类似的问题,但我早已忘记了。可以这样解决这个问题iostream'scout

#include<iostream>
#include<stdio.h>

main() {
    unsigned char a;
    a=1;
    printf("%d", a);
    std::cout<< +a << std::endl;

    return 0;
}

instead of casting it back to another type if you want coutto print the unsigned charvalue as opposed to the asciicharacter. You need to promoteit.

如果要cout打印unsigned char值而不是ascii字符,请不要将其转换回另一种类型。你需要promote它。

If you noticed all I did was add a +before the unsigned char. This is unary addition that will promote the unsigned charto give you the actual number representation.

如果您注意到我所做的只是+unsigned char. 这是一元加法,它将促进unsigned char为您提供实际的数字表示。

User Baum mit Augen is responsible for reminding me of this solution.

用户 Baum mit Augen 负责提醒我这个解决方案。

回答by Ganesh

You need to typecast aas integer as cout<< (int)(a);. With this you will observe 1on the output. With cout << a;, the print will be SOH (Start of Heading)corresponding to ascii value of 1which can't be printed and hence, some special character is observed.

您需要将a整数类型转换为cout<< (int)(a);. 有了这个,您将观察1输出。使用cout << a;,打印将SOH (Start of Heading)对应无法打印的ascii 值,1因此,观察到一些特殊字符。

EDIT:

编辑

To be more accurate, the coutstatement should be cout << static_cast<unsigned>(a)as Nawaz has mentioned.

更准确地说,该cout声明应cout << static_cast<unsigned>(a)如 Nawaz 所提到的那样。

回答by R1S8K

The C compiler has its own way of defining the type of the printed output, because you can specify the type of the output.

C 编译器有自己定义打印输出类型的方法,因为您可以指定输出类型。

Ex:

前任:

uint8_t c = 100;
printf("%d",c);

so you can also print cas an int by %d, or char %c, string %sor a hex value %x.

所以你也可以打印c为 int by %d, or char %c, string%s或 hex value %x

Where C++ has its own way too, the coutprints the 8-bit values as a charby default. So, you have to use specifiers with the output argument.

C++ 也有自己的方式,默认情况下cout打印 8 位值char。因此,您必须在输出参数中使用说明符。

You can either use:

您可以使用:

  1. a +before the name of the output argument

    uint8_t data_byte = 100;
    cout << "val: " << +data_byte << endl;
    
  2. use a function cast unsigned(var); like,

     uint8_t data_byte = 100;
     cout << "val: " << unsigned(data_byte) << endl;
    
  1. +输出参数名称之前的a

    uint8_t data_byte = 100;
    cout << "val: " << +data_byte << endl;
    
  2. 使用函数转换unsigned(var);喜欢,

     uint8_t data_byte = 100;
     cout << "val: " << unsigned(data_byte) << endl;
    

回答by Jagathesan NB

printf("%u",a); its so simple try it

printf("%u",a); 就这么简单试试