[英]ASCII Dec to Char in C++
我想以普通字符獲取ASCII的每個字符。 如果我只輸入char key
,它將返回dec。
我的請求:
char alph = //ascii dec to normal char
例如:dec中的A是65注意:我沒有字符,但確實有dec中的ASCII碼,例如65。
因為我需要像65這樣的用戶輸入
在這種情況下,您可以執行以下操作:
#include <iostream>
using namespace std;
int main() {
int code;
cout << "Enter a char code:" << endl;
cin >> code;
char char_from_code = code;
cout << char_from_code << endl;
return 0;
}
這將輸出:
Enter a char code:
65
A
看來您誤解了這個概念。
數值始終存在。 將其打印為字母還是數字取決於打印方式。
std :: cout會將字符打印為字母(又稱字符),因此您需要將其轉換為其他整數類型以打印值。
char c = 'a';
cout << c << endl; // Prints a
cout << (uint32_t)c << endl; // Prints 97
cout << endl;
uint32_t i=98;
cout << i << endl;
cout << (char)i << endl;
輸出:
a
97
98
b
這是方法,非常簡單,然后只需創建自己的用戶界面即可獲得輸入dec
#include <iostream>
using namespace std;
int main() {
int dec = 65;
cout << char(dec);
cin.get();
return 0;
}
看起來您需要十六進制/非十六進制轉換器。 快來看看,或使用這輛自行車:
vector<unsigned char> dec2bin( const string& _hex )
{
vector<unsigned char> ret;
if( _hex.size() < 2 )
{
return ret;
}
for( size_t i = 0; i <= _hex.size() - 2; i += 2 )
{
string two = string( _hex.data() + i, 2 );
stringstream ss( two );
string ttt = ss.str();
int tmp;
ss >> /*hex >>*/ tmp;
unsigned char c = (unsigned char)tmp;
ret.insert( ret.end(), c );
}
return ret;
}
int main()
{
string a = "65";
unsigned char c = dec2bin( a )[0];
cout << (char)c << endl;
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.