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
cout not printing unsigned char
提问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 cout
is behaving so?
为什么cout
行为如此?
回答by Nawaz
cout << a
is 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 1
is non-printable. You can check whether a
is printable or not using, std::isprint
as:
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 cout
to print 1
also, then cast a
to 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's
cout
can be done like this:
我在这里遇到了类似的问题,但我早已忘记了。可以这样解决这个问题iostream's
cout
:
#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 cout
to print the unsigned char
value as opposed to the ascii
character. You need to promote
it.
如果要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 char
to 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 a
as integer as cout<< (int)(a);
. With this you will observe 1
on the output. With cout << a;
, the print will be SOH (Start of Heading)
corresponding to ascii value of 1
which 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 cout
statement 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 c
as an int by %d
, or char %c
, string %s
or a hex value %x
.
所以你也可以打印c
为 int by %d
, or char %c
, string%s
或 hex value %x
。
Where C++ has its own way too, the cout
prints the 8-bit values as a char
by default. So, you have to use specifiers with the output argument.
C++ 也有自己的方式,默认情况下cout
打印 8 位值char
。因此,您必须在输出参数中使用说明符。
You can either use:
您可以使用:
a
+
before the name of the output argumentuint8_t data_byte = 100; cout << "val: " << +data_byte << endl;
use a function cast
unsigned(var)
; like,uint8_t data_byte = 100; cout << "val: " << unsigned(data_byte) << endl;
+
输出参数名称之前的auint8_t data_byte = 100; cout << "val: " << +data_byte << endl;
使用函数转换
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); 就这么简单试试