简体   繁体   English

如何将C++中的ASCII值转换成char?

[英]How to convert ASCII value into char in C++?

How do I convert 5 random ascii values into chars?如何将 5 个随机 ascii 值转换为字符?


Prompt:迅速的:

Randomly generate 5 ascii values from 97 to 122 (the ascii values for all of the alphabet).随机生成 5 个从 97 到 122 的 ascii 值(所有字母表的 ascii 值)。 As you go, determine the letter that corresponds to each ascii value and output the word formed by the 5 letters.如你go,确定每个ascii值对应的字母和output这5个字母组成的单词。

My Code:我的代码:

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main ()
{
srand (time(NULL));
int val1= rand()%122+97;
int val2= rand()%122+97;
int val3= rand()%122+97;
int val4= rand()%122+97;
int val5= rand()%122+97

cout<<val1<<" and "<<val2<<" and "<<val3<<" and "<<val4<<" and "<<val15<<". "<<






return 0;
}
for (int i = 0; i < 5; i++){
    int asciiVal = rand()%26 + 97;
    char asciiChar = asciiVal;
    cout << asciiChar << " and ";
}

To convert an int ASCII value to character you can also use: 要将int ASCII值转换为字符,您还可以使用:

int asciiValue = 65;
char character = char(asciiValue);
cout << character; // output: A
cout << char(90); // output: Z
int main() 
{

    int v1, v2, v3, v4, v5,v6,v7;
    cout << "Enter 7 vals ";
    cin >> v1 >> v2 >> v3 >> v4 >> v5 >> v6 >> v7;

    cout << "The phrase is " 
         << char(v1) 
         << char(v2) << " "
         << char(v3) << " "
         << char(v4) 
         << char(v5) 
         << char(v6)  
         << char(v7);

        system("pause>0");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM