std::cout 輸出 unsigned char型別資料
寫C++程式碼的時候可能發現,輸出char, float, int, 還有double 等型別的時候正常,但是遇到unsigned char有時候就輸出亂碼。
所以自己測試一下:
int main(int argc, char** argv)
{
std::cout << "test for uchar" << std::endl;
int h = 255;
//unsigned char u = (unsigned char)(h);
unsigned char u = '255';
char c = 'a';
float f = 1.0f;
double d = 1.0f;
int i = 1;
std::cout << "unsigned char: " << u << std::endl;
std::cout << "char: " << c << std::endl;
std::cout << "float: " << f << std::endl;
std::cout << "double: " << d << std::endl;
std::cout << "int: " << i << std::endl;
}
輸出的結果正常:
unsigned char 一般是0-255之間,當我們賦值的方式給予一個值的時候:
int main(int argc, char** argv)
{
std::cout << "test for uchar" << std::endl;
int h = 255;
unsigned char u = (unsigned char)(h);
//unsigned char u = '255';
char c = 'a';
float f = 1.0f;
double d = 1.0f;
int i = 1;
std::cout << "unsigned char: " << u << std::endl;
std::cout << "char: " << c << std::endl;
std::cout << "float: " << f << std::endl;
std::cout << "double: " << d << std::endl;
std::cout << "int: " << i << std::endl;
}
輸出的結果為垃圾值:
It will not work because here u(unsigned char) is a unsigned char (with value 255), cout actually will print some garbage value (or nothing) as it is just a non-printable ASCII character which is getting printed anyway. Note that ASCII character corresponding to 255 is non-printable. Check out here for the ASCII table.
P.S. You can check whether u is printable or not using isprint() as
上面的意思是:當賦予這個超過了ASCII的範圍了,所以列印不出來,或者是垃圾值。
最後測試:
當賦予u為正常的正常的值的時候,97對應為’a’,輸出為‘a’,測試結果如下:
測試程式碼:
int main(int argc, char** argv)
{
std::cout << "test for uchar" << std::endl;
int h = 97;
unsigned char u = (unsigned char)(h);
//unsigned char u = '255';
char c = 'a';
float f = 1.0f;
double d = 1.0f;
int i = 1;
std::cout << "unsigned char: " << u << std::endl;
std::cout << "char: " << c << std::endl;
std::cout << "float: " << f << std::endl;
std::cout << "double: " << d << std::endl;
std::cout << "int: " << i << std::endl;
}
測試結果
所以,std::cout出不來的時候,可以用強制轉換為int,cout就可以了。
程式碼如下:
int main(int argc, char** argv)
{
std::cout << "test for uchar" << std::endl;
int h = 255;
unsigned char u = (unsigned char)(h);
//unsigned char u = '255';
char c = 'a';
float f = 1.0f;
double d = 1.0f;
int i = 1;
std::cout << "unsigned char: " << (int)u << std::endl;
std::cout << "char: " << c << std::endl;
std::cout << "float: " << f << std::endl;
std::cout << "double: " << d << std::endl;
std::cout << "int: " << i << std::endl;
}
測試的結果:
參見的材料如下:
https://www.it1352.com/457126.html
相關文章
- JavaScript資料型別及輸出JavaScript資料型別
- MySQL資料型別操作(char與varchar)MySql資料型別
- ABAP中Char型別資料轉換成Decimal型別型別Decimal
- c++ cout 格式輸出C++
- MySQL CHAR和VARCHAR資料型別介紹MySql資料型別
- Oracle的資料型別:char/varchar2Oracle資料型別
- MySQL字元資料型別char與varchar的區別MySql字元資料型別
- [java IO流]之 基本資料型別輸入輸出流Java資料型別
- 用printf輸出string型別資料總結型別
- iOS資料型別簡介及NSLog列印輸出iOS資料型別
- PLSQL Language Reference-PL/SQL資料型別-SQL資料型別-CHAR和VARCHAR2變數SQL資料型別變數
- C++輸入輸出常用格式(cin,cout,stringstream)C++
- Oracle 資料型別CHAR, NCHAR, VARCHAR2, NVARCHAR2Oracle資料型別
- const char* 型別不能用於初始化char* 型別實體型別
- printf 中 % 別代表的輸出型別型別
- 避免使用Oracle的char型別Oracle型別
- C++ unsigned char*轉化為string的形式C++
- CAPL指令碼中常用到的資料型別轉換——數字型別(int/double)和字串型別(char array)指令碼資料型別字串
- char型別的數值轉換型別
- js資料型別之基本資料型別和引用資料型別JS資料型別
- 資料型別: 資料型別有哪些?資料型別
- 【C++】CONST CHAR*型別的值不能用於初始化CHAR*型別的實體C++型別
- C++輸出流cout的執行順序問題C++
- C++初始化,之不明白篇 cout<<x<<endl 與 cout<<"x = "<<cout<<x<<endl的輸出的值會不一樣C++
- int型別按位元組列印輸出型別
- MySQL基礎教程---Chapter 5 資料型別和資料輸入MySqlAPT資料型別
- NTMySQL中varchar和char型別的區別heeMySql型別
- std::vector 和 std::list 區別
- 強資料型別和弱資料型別資料型別
- MySQL中資料型別(char(n)、varchar(n)、nchar(n)、nvarchar(n)的區別)MySql資料型別
- 區別值型別資料和引用型別資料型別
- 應用中避免使用char/nchar型別型別
- PL/SQL中char型別的暗門SQL型別
- 【c語言】實現對一個8bit資料(unsigned char 型別)的指定位(例如第n位)置0或者置1操作,並保持其他位不變C語言型別
- 資料的輸入輸出
- 03 資料輸入-輸出
- 輸出C語言中 變數的型別C語言變數型別
- 資料型別,型別轉換資料型別