[英]cout not printing unsigned char
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
表现如此?
cout << a
is printing a value which appears to be garbage to you. cout << a
正在打印一个对您来说似乎是垃圾的值。 It is not garbage actually.其实不是垃圾。 It is just a non-printable ASCII character which is getting printed anyway.
它只是一个不可打印的ASCII 字符,无论如何都会被打印出来。 Note that ASCII character corresponding to
1
is non-printable.请注意,对应于
1
ASCII 字符是不可打印的。 You can check whether a
is printable or not using, std::isprint
as:您可以使用
std::isprint
来检查a
是否可打印:
std::cout << std::isprint(a) << std::endl;
It will print 0
(read: false
) indicating the character is non-printable它将打印
0
(read: 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;
I had a similar issue here that 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.如果您希望
cout
打印unsigned char
值而不是ascii
字符, ascii
要将其转换回另一种类型。 You need to promote
it.你需要
promote
它。
If you noticed all I did was add a +
before the unsigned char
.如果您注意到我所做的只是在
unsigned char
之前添加一个+
。 This is unary addition that will promote the unsigned char
to give you the actual number representation.这是一元加法,将提升
unsigned char
以提供实际数字表示。
User Baum mit Augen is responsible for reminding me of this solution.用户 Baum mit Augen 负责提醒我这个解决方案。
You need to typecast a
as integer as cout<< (int)(a);
您需要将
a
作为整数类型转换为cout<< (int)(a);
. . With this you will observe
1
on the output.有了这个,您将在输出中观察到
1
。 With cout << a;
随着
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. ,打印将是对应于无法打印的 ascii 值
1
SOH (Start of Heading)
,因此,观察到一些特殊字符。
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 所提到的。
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 %d
或char %c
,串%s
或十六进制值%x
。
Where C++ has its own way too, the cout
prints the 8-bit values as a char
by default. C++ 也有自己的方式,默认情况下
cout
将 8 位值打印为char
。 So, you have to use specifiers with the output argument.因此,您必须在输出参数中使用说明符。
You can either use:您可以使用:
a +
before the name of the output argument输出参数名称前的
+
uint8_t data_byte = 100; cout << "val: " << +data_byte << endl;
use a function cast unsigned(var)
;使用函数 cast
unsigned(var)
; like,喜欢,
uint8_t data_byte = 100; cout << "val: " << unsigned(data_byte) << endl;
printf("%u",a); printf("%u",a); its so simple try it
就这么简单试试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.