How to turn each character of a string into its ASCII character code in C++

1 Answer

0 votes
#include <iostream>

int main() {
    std::string s = "c++ programming";
    
    int len = s.length();
    for (int i = 0; i < len; i++) {
        int ascii = s[i] - 0;
        std::cout << ascii << ' ';
    }
     
    return 0;
}



/*
run:

99 43 43 32 112 114 111 103 114 97 109 109 105 110 103 

*/

 



answered Apr 2, 2021 by avibootz
...