[英]Strange output when printing char assigned to hex value using std::cout
[英]Getting a '?' in the Output when printing special (Extended) ASCII characters using `std::cout`
我想在控制台中打印擴展的 ascii 字符,我試過這個:
std::cout << "╔══════════╦════════════════════════════════════════════════════════╗\n";
std::cout << "║ ID:678 ║ Name: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ Surname: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ Gender: ▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ Age: ▓▓▓ ║\n";
std::cout << "║ ║ Tel: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ Adress: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ID Print ║ Hobbies: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ║ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ░░░░░░ ║ Additional İnformations: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ▒▓▓▓▓▒ ║ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "║ ▓▓▓▓▓▓ ║ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ║\n";
std::cout << "╚══════════╩════════════════════════════════════════════════════════╝\n";
但輸出是這樣的:
?????????????????????????????????????????????????????????????????????
? ID:678 ? Name: ???????????????????????????????????????? ?
? ? Surname: ???????????????????????????????????????? ?
? ? Gender: ?????? ?
? ? Age: ??? ?
? ? Tel: ??????????????????????????????? ?
? ? Adress: ????????????????????????????????????????? ?
? ? ????????????????????????????????????????????????? ?
? ID Print ? Hobbies: ???????????????????????????????????????? ?
? ? ????????????????????????????????????????????????? ?
? ?????? ? Additional ¦nformations: ???????????????????????? ?
? ?????? ? ????????????????????????????????????????????????? ?
? ?????? ? ????????????????????????????????????????????????? ?
?????????????????????????????????????????????????????????????????????
但是當我嘗試這個時: std::cout << char(200);
它成功打印了 '╔' 字符。 所以我確定控制台支持擴展的 ascii 字符。
所以; 我怎樣才能正確輸出這些 cout?
編輯:使用std::wcout << L"";
不工作。
Edit2:程序只能在 Windows 7 或更高版本中運行。
編輯3:
使用此代碼打印卡片,
#include <fcntl.h>
#include <io.h>
_setmode(_fileno(stdout), _O_U16TEXT);
但是,我無法打印任何正常的 cout 字符,它會出錯。 (示例錯誤字符串: std::cout << "Hello!"
)
對於在windows上寫過的人,可以應用這個:source( _setmode Windows source )
需要的庫:
#include <fcntl.h>
#include <io.h>
將 cout 轉換為 Unicode:
_setmode(_fileno(stdout), _O_U16TEXT);
*重要提示:在這種風格中,你總是應該使用 std:: w cout << L""; 為了這。 如果您的代碼在其他行中包含 std::cout,則解決方案如下:
使用_setmode(_fileno(stdout), _O_TEXT);
當您不需要特殊字符進行打印時,將 stdout 設置回默認文本樣式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.