返回列表 發帖

字串處理 (八) - 將字串轉換為ASCII碼

本帖最後由 鄭繼威 於 2023-3-15 17:38 編輯



int(字元) --> ASCII碼
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     char str[100];
  7.     cout<<"請輸入一字串(包含空白,100字內): ";
  8.     cin.getline(str,100);
  9.     cout<<"您剛輸入的字串ASCII碼依序為: "<<endl;
  10.     for(int i=0; str[i]!=NULL; i++)
  11.         cout<<int(str[i])<<endl;
  12.     system("pause");
  13.     return 0;   
  14. }
複製代碼
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str;
  8.     cout<<"請輸入一字串(包含空白,100字內): ";
  9.     getline(cin,str);
  10.     cout<<"您剛輸入的字串ASCII碼依序為: "<<endl;
  11.     for(int i=0; i<str.length(); i++)
  12.         cout<<int(str[i])<<endl;
  13.     system("pause");
  14.     return 0;   
  15. }
複製代碼

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     char str[100];
  7.     cout<<"請輸入一字串(包含空白,100字內): ";
  8.     cin.getline(str,100);
  9.     cout<<"您剛輸入的字串ASCII碼依序為: "<<endl;
  10.     for(int i=0; str[i]!=NULL; i++)
  11.         cout<<int(str[i])<<endl;
  12.     system("pause");
  13.     return 0;   
  14. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.     string str;
  8.     cout<<"請輸入一字串(包含空白,100字內): ";
  9.     getline(cin,str);
  10.     cout<<"您剛輸入的字串ASCII碼依序為: "<<endl;
  11.     for(int i=0; i<str.length(); i++)
  12.         cout<<int(str[i])<<endl;
  13.     system("pause");
  14.     return 0;   
  15. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;

  4. int main()
  5. {
  6.     string str;
  7.     cout<<"輸入一串字串:";
  8.     getline(cin,str);
  9.     cout<<"您剛輸入的字串ASCII碼依序為:"<<endl;
  10.     for(int i=0; i<str.size(); i++)
  11.     {
  12.         cout<<int(str[i])<<endl;
  13.     }
  14.     system("pause");
  15.     return 0;
  16. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;

  4. int main()
  5. {
  6.     string str;
  7.    cout<<"請輸入一字串(包含空白,100字內): ";
  8.     getline(cin,str);
  9.     cout<<"您剛輸入的字串ASCII碼依序為: "<<endl;
  10.       for(int i=0; i<str.length(); i++)
  11.         cout<<int(str[i])<<endl;   
  12.     system("pause");
  13.     return 0;
  14. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string.h>
  4. using namespace std;
  5. int main()
  6. {
  7.         char c;
  8.     cout<<"請輸入你要查詢ASCII的字元: ";
  9.     cin>>c;
  10.     cout<<"字元<"<<c<<">的查詢結果:"<<int(c)<<endl;        //char->int

  11.     return 0;   
  12. }
複製代碼

TOP

返回列表