将数字分配给C中的char变量?

时间:2018-07-11 17:04:24

标签: c char int

我打算将一个数字数据类型分配给char变量。 该代码将是这样的:

#include <stdlib.h>
#include <stdio.h>

typedef unsigned short ushort;
typedef unsigned u;

ushort getRandomNumber(){
    return (ushort)(rand() % 9);
}

ushort getASCII(char Char) {
    return (ushort)Char;
}

char getRandomChar() {
    ushort chance = (rand() % 2) + 1;
    const ushort aASCII = 97 , zASCII = 122 , AASCII = 65 , ZASCII = 89;
    if(chance == 1)
        return (char)(aASCII + (rand() % (zASCII - aASCII)));
    else
        return (char)(AASCII + (rand() % (ZASCII - AASCII)));
}

int main(void) {
    srand(time(NULL));
    ushort size;
    puts("Enter password size : " );
    fflush(stdout);
    scanf("%i" , &size);
    if(size >= 4) {
    char Password[size + 1];
        for(ushort i = 1 ; i <= size ; ++i){
            ushort chance = (rand() % 2) + 1;
                if(chance == 1)
                    Password[i] = getRandomChar();
                else
                    Password[i] = getRandomNumber();

        }
    printf("%s" , Password);
    return EXIT_SUCCESS;
    }
    else {
        puts("Error. Try again");
    }
}

它不会返回任何错误,但是会打印与ASCII码匹配的字符。因此,此代码不是正确的代码。

原始代码是一个随机密码生成器,它不是很好,而且我知道它是安全的。随机数在输出中显示为ASCII字符。

我在Google上搜索,但没有有用的结果。仅this,但这是相反的过程。 的命令/算法是什么?

感谢您的帮助。

编辑:这里的问题是我将int分配给具有不想显示为数字的字符的char数组。

3 个答案:

答案 0 :(得分:3)

由于您实际所做的是用字母或一位数字填充字符串,因此您实际上并不希望自己存储数字,而是要存储数字的ASCII码。

您可以执行以下操作:

ushort getRandomNumber(){
    return '0' + (ushort)(rand() % 10);
}

请注意,对于ASCII码,使用字符常量而不是“幻数”。获取角色时,您可以进行类似的更改:

char getRandomChar() {
    ushort chance = (rand() % 2) + 1;
    if(chance == 1)
        return 'a' + (rand() % ('z'- 'a' + 1));
    else
        return 'a' + (rand() % ('Z' - 'A' + 1));
}

C标准保证,无论使用哪种字符编码,0-9的数字都是连续的。但是,对于字母,不存在该保证。

通过创建要使用的所有字符的数组并将其索引到该数组中以获得给定的随机字符,可以进一步简化逻辑:

char characters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for(ushort i = 1 ; i <= size ; ++i){
        Password[i] = characters[rand() % sizeof(characters)];
    }
    Password[i] = 0;

答案 1 :(得分:2)

我认为您希望将类型从int转换为char,但是由于我们一次只能将一位数字转换为char,因此我们必须提取所有数字并将其存储在字符串中,然后反转字符串以获得答案。 / p>

希望这段代码可以给您一些想法

#include <stdio.h>
#include <stdlib.h>
#define max 10000
int main(void){
    int a = 12233;
    char b[max];             // Create a char array of max size
    int i=0,j;
    while(a)
    {
        int rem = a%10;            // since we can only convert single int digit to char use loop to extract digits
        b[i] = rem + '0';          // and convert into char
        i++;
        a = a/10;
    }
    b[i] = '\0';                  // don't forget to insert '\0' to make it string
    for(i=0,j=strlen(b)-1;i<=j;i++,j--)        // now we have char array like 21, so we have to reverse it
    {
        char temp = b[i];
        b[i] = b[j];
        b[j] = temp;
    }

    puts(b);                       // print string
    return 0;
}

答案 2 :(得分:0)

使用 printf()会有所帮助,puts()用于将字符串打印到控制台

#include <stdio.h>
#include <stdlib.h>
int main(void){
    int a = 12;
    char b = a;
    printf("%d",b);
    return 0;
}