开发者

how to convert an integer value to a specific ascii character in c++

开发者 https://www.devze.com 2023-01-09 02:12 出处:网络
I\'m new to C++ and I\'m trying to do something that should be pretty basic. I have a small loop in C++ that just displays a sequence of numbers and I would like to convert these numbers into specifi

I'm new to C++ and I'm trying to do something that should be pretty basic.

I have a small loop in C++ that just displays a sequence of numbers and I would like to convert these numbers into specific ASCII characters. Something like this:

    for (int k = 0; k开发者_StackOverflow < 16; k++) {
        display(65+k);
    }

And the result should look like this:

ABCDEFGH... etc

Any ideas?

Thanks!


EDIT based on clarification: Judging from the error message display takes a C-style string. You can build one like this:

for (int k = 0; k < 16; k++) {
    char str[2] = { 65 + k };  // Implicitly add the terminating null.
    display(str);
}


That would be

#include <iostream>  
int main() {  
for (int k = 0; k < 16; k++) {
        std::cout.put(65+k);
    }
}

for C++

0

精彩评论

暂无评论...
验证码 换一张
取 消