cint로 uint8_t를 인쇄 할 수 없습니다
C ++에서 정수로 작업하는 데 이상한 문제가 있습니다.
값을 변수로 설정하고 인쇄하는 간단한 프로그램을 작성했지만 예상대로 작동하지 않습니다.
내 프로그램에는 두 줄의 코드 만 있습니다.
uint8_t aa = 5;
cout << "value is " << aa << endl;
이 프로그램의 출력은 value is
즉,에 공백으로 인쇄됩니다 aa
.
내가 변경하는 경우 uint8_t
에 uint16_t
위의 코드를 마치 마법처럼 작동합니다.
64 비트 Ubuntu 12.04 (Precise Pangolin)를 사용하며 컴파일러 버전은 다음과 같습니다.
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
실제로 공백을 인쇄하지는 않지만 값이 5 인 ASCII 문자는 인쇄 할 수 없거나 보이지 않습니다. 보이지 않는 ASCII 문자 코드 가 여러 개 있는데 , 대부분이 실제로는 공백 인 값 32 미만입니다.
보이는 문자 값을 출력하려고하기 때문에 숫자 값을 출력하려면 로 변환 aa
해야 합니다.unsigned int
ostream& operator<<(ostream&, unsigned char)
uint8_t aa=5;
cout << "value is " << unsigned(aa) << endl;
uint8_t
에 typedef
대한 가능성이 높습니다 unsigned char
. 이 ostream
클래스에는에 대한 특수 과부하가 있습니다 unsigned char
. 즉 인쇄 할 수없는 숫자 5로 문자를 인쇄하므로 빈 공간이됩니다.
기본 데이터 유형의 변수 앞에 단항 + 연산자를 추가하면 ASCII 문자 (문자 유형의 경우) 대신 인쇄 가능한 숫자 값이 제공됩니다.
uint8_t aa=5;
cout<<"value is "<< +aa <<endl;
출력 연산자는 ( 일반적으로에 대한 별칭 일뿐) uint8_t
처럼 취급 하므로 ASCII 코드 (가장 일반적인 문자 인코딩 시스템)로 문자를 인쇄합니다 .char
uint8_t
unsigned char
5
예를 들어이 참조를 참조하십시오 .
의 사용을 만들기 ADL (인수 종속적 이름 조회를)
#include <cstdint> #include <iostream> #include <typeinfo> namespace numerical_chars { inline std::ostream &operator<<(std::ostream &os, char c) { return std::is_signed<char>::value ? os << static_cast<int>(c) : os << static_cast<unsigned int>(c); } inline std::ostream &operator<<(std::ostream &os, signed char c) { return os << static_cast<int>(c); } inline std::ostream &operator<<(std::ostream &os, unsigned char c) { return os << static_cast<unsigned int>(c); } } int main() { using namespace std; uint8_t i = 42; { cout << i << endl; } { using namespace numerical_chars; cout << i << endl; } }
산출:
* 42
커스텀 스트림 매니퓰레이터도 가능합니다.
- 단항 더하기 연산자는 깔끔한 관용구입니다 (
cout << +i << endl
).
cout
인쇄 할 수없는 문자 인 ASCII 값 aa
으로 처리하고 있으므로 인쇄 하기 전에 형식 변환을 시도하십시오 .char
5
int
operator<<()
사이 과부하 istream
와 char
비 멤버 함수이다. 멤버 함수를 명시 적으로 사용하여 char
(또는 a uint8_t
)를로 취급 할 수 있습니다 int
.
#include <iostream>
#include <cstddef>
int main()
{
uint8_t aa=5;
std::cout << "value is ";
std::cout.operator<<(aa);
std::cout << std::endl;
return 0;
}
산출:
value is 5
표준 스트림은 부호있는 문자와 부호없는 문자를 숫자가 아닌 단일 문자로 취급하기 때문에 문제가 발생하기 전에 다른 사람들이 말한 것처럼.
다음은 코드 변경을 최소화 한 솔루션입니다.
uint8_t aa = 5;
cout << "value is " << aa + 0 << endl;
"+0"
부동 소수점을 포함하여 어떤 숫자로도 추가하는 것이 안전합니다.
For integer types it will change type of result to int
if sizeof(aa) < sizeof(int)
. And it will not change type if sizeof(aa) >= sizeof(int)
.
This solution is also good for preparing int8_t
to be printed to stream while some other solutions are not so good:
int8_t aa = -120;
cout << "value is " << aa + 0 << endl;
cout << "bad value is " << unsigned(aa) << endl;
Output:
value is -120
bad value is 4294967176
P.S. Solution with ADL given by pepper_chico and πάντα ῥεῖ is really beautiful.
참고URL : https://stackoverflow.com/questions/19562103/uint8-t-cant-be-printed-with-cout
'Programing' 카테고리의 다른 글
BeautifulSoup과 Scrapy 크롤러의 차이점은 무엇입니까? (0) | 2020.07.10 |
---|---|
Java를 사용하여 스크린 샷을 찍어 일종의 이미지로 저장하는 방법이 있습니까? (0) | 2020.07.10 |
ctrl-c 이벤트를 어떻게 잡을 수 있습니까? (0) | 2020.07.10 |
Markdown Syntax를 사용하여 인용구의 저자 인용 (0) | 2020.07.10 |
Numpy array, 여러 조건을 만족하는 인덱스를 선택하는 방법은 무엇입니까? (0) | 2020.07.10 |