將 ASCII 值句子轉換為等效的 C++ 字串


在本教程中,我們來探討一個程式,該程式將 ASCII 值句子轉換為其等效字串。

為此,我們會提供一個包含 ASCII 程式碼的字串。我們的任務是將給定的字串轉換為等效字元並將其列印回來。

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
//converting the ASCII sequence into
//character string
void convert_ASCII(string str, int len){
   int num = 0;
   for (int i = 0; i < len; i++) {
      //appending the current digit
      num = num * 10 + (str[i] - '0');
      //checking if number is within range
      if (num >= 32 && num <= 122) {
         char ch = (char)num;
         cout << ch;
         num = 0;
      }
   }
}
int main(){
   string str = "104101108108111443211911111410810033";
   int len = str.length();
   convert_ASCII(str, len);
   return 0;
}

輸出

hello, world!

更新於:2020 年 1 月 22 日

698 次瀏覽

啟動 職業生涯

透過完成課程獲得認證

開始
廣告