返回列表 發帖

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



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

TOP

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

  5. int main()
  6. {
  7.     string a;
  8.     getline(cin,a);
  9.     for(int i=0;i<a.length();i++)
  10.     {
  11.         cout<<int(a[i])<<endl;
  12.     }
  13. }
複製代碼

TOP

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4.     string let;
  5.     cout<<"Enter Something: ";
  6.     getline(cin,let);
  7.     cout<<"ASCII output= "<<endl;
  8.     for(int i=0;i<let.length();i++)
  9.     {
  10.             cout<<int(let[i])<<endl;
  11. }

  12.    

  13.     system("pause");
  14.     return 0;
  15. }
複製代碼

TOP

本帖最後由 蔡沛倢 於 2023-10-6 19:25 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     string a;
  7.     cout<<"請輸入任一一組字串(包含空白,100字內):";
  8.     getline(cin,a);
  9.     cout<<"你輸入的字串ASCII碼依序為:"<<endl;
  10.     for(int i=0;i<a[i];i++)
  11.     {
  12.         cout<<int(a[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.     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.h>
  4. using namespace std;
  5. int main()
  6. {
  7.     char s[1000];
  8.     cout<<"輸入一字串:";
  9.     cin.getline(s,1000);
  10.     int counter=0;
  11.     cout<< "你剛剛輸入字串的ASCII碼依序為:"<<endl;
  12.     for(int i=0;s[i]!=NULL;i++)
  13.         cout<<int(s[i])<<endl;

  14.     system("pause");
  15.     return 0;
  16. }
複製代碼

TOP

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

TOP

#include <iostream>
#include <string.h>
#include <cstdlib>
using namespace std;

int main(){
    string s2;
    getline(cin, s2);
    int n = s2.length();
    for(int i = 0;i<=n-1;i++){
        cout << int(s2[i]) << endl;
    }
    return 0;
}

TOP

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

TOP

  1. #include<cstdlib>
  2. #include<iostream>
  3. using namespace std;
  4. int main(){
  5.     cout<<"wassup?";
  6.     string str;
  7.     getline(cin,str);
  8.     cout<<str.length()<<" characters!"<<endl;
  9.     for(int x=0;x<str.size();x++){
  10.             cout<<int(str[x])<<endl;
  11.             }

  12.     system("pause");
  13.     return 0;
  14. }
複製代碼
Attention Seeker </3

TOP

返回列表