Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将ascii码(整数)转换为字符_C - Fatal编程技术网

将ascii码(整数)转换为字符

将ascii码(整数)转换为字符,c,C,我正在尝试将整数列表转换为字符(整数由空格、换行符和制表符分隔)。输入以EOF结束。例如 输入; 72 101 108 108 111 44 32 119 111 114 108 100 33 输出 你好,世界 #include <stdio.h> #include <ctype.h> #define MAXBUFFERSIZE 100 void cleartoendofline( void ); /* ANSI function prototype */ vo

我正在尝试将整数列表转换为字符(整数由空格、换行符和制表符分隔)。输入以EOF结束。例如 输入; 72 101 108 108 111 44 32 119 111 114 108 100 33 输出 你好,世界

#include <stdio.h>
#include <ctype.h>
#define MAXBUFFERSIZE   100

void cleartoendofline( void );  /* ANSI function prototype */

void cleartoendofline( void )
{
    char ch;
        ch != '\n';
    //ch = getchar();
    //while( ch != '\n' )
        //ch = getchar();
}

main()
{
    char    ch;                     /* handles user input */
    char    buffer[MAXBUFFERSIZE];  /* sufficient to handle one line */
    int     char_count;             /* number of characters read for this line */
    int     exit_flag = 0;
    int     valid_choice;

    while( exit_flag  == 0 ) {
        printf("Enter integer(s)\n: ");
        //ch = getchar();
                scanf("%d",&ch)
        char_count = 0;
        while( (ch != '\n')  &&  (char_count < MAXBUFFERSIZE)) {
            buffer[char_count++] = ch;
            ch = getchar();
        }
        buffer[char_count] = 0x00;      /* null terminate buffer */
        printf("\nIntegers translates to:\n");
        printf("%s\n", buffer);

        valid_choice = 0;
        while( valid_choice == 0 ) {
            printf("Continue (Y/N)?\n");
            scanf(" %c", &ch );
            ch = toupper( ch );
            if((ch == 'Y') || (ch == 'N') )
                valid_choice = 1;
            else
                printf("\007Error: Invalid choice\n");
            cleartoendofline();
        }
        if( ch == 'N' ) exit_flag = 1;
    }
}
#包括
#包括
#定义MAXBUFFERSIZE 100
void cleartoendofline(void);/*ANSI函数原型*/
void cleartoendofline(void)
{
char ch;
ch!='\n';
//ch=getchar();
//而(ch!='\n')
//ch=getchar();
}
main()
{
char ch;/*处理用户输入*/
字符缓冲区[MAXBUFFERSIZE];/*足以处理一行*/
int char_count;/*此行读取的字符数*/
int exit_标志=0;
int有效_选择;
while(exit_标志==0){
printf(“输入整数)\n:”;
//ch=getchar();
scanf(“%d”和“ch”)
字符计数=0;
而((ch!='\n')&&(字符计数
已修复,例如

#include <stdio.h>
#include <ctype.h>
#define MAXBUFFERSIZE   100

void cleartoendofline( void );  /* ANSI function prototype */

void cleartoendofline( void ){
    while('\n'!=getchar());
}

main(){
    char    ch;                     /* handles user input delimiter*/
    int     num;                    /* user input number */
    char    buffer[MAXBUFFERSIZE];  /* sufficient to handle one line */
    int     char_count;             /* number of characters read for this line */
    int     exit_flag = 0;
    int     valid_choice;

    while( exit_flag  == 0 ) {
        printf("Enter integer(s)\n: ");
        scanf(" %d%c", &num, &ch);//read integer and delimiter
        char_count = 0;
        while( char_count < MAXBUFFERSIZE - 1) {//-1 for End of string
            buffer[char_count++] = (char)num;
            if(ch == '\n')break;
            scanf("%d%c", &num, &ch);
        }
        buffer[char_count] = 0x00;      /* null terminate buffer */
        printf("\nIntegers translates to:\n");
        printf("%s\n", buffer);

        valid_choice = 0;
        while( valid_choice == 0 ) {
            printf("Continue (Y/N)?\n");
            scanf(" %c", &ch );
            ch = toupper( ch );
            if((ch == 'Y') || (ch == 'N') )
                valid_choice = 1;
            else
                printf("\aError: Invalid choice\n");
            cleartoendofline();
        }
        if( ch == 'N' ) exit_flag = 1;
    }
}
#包括
#包括
#定义MAXBUFFERSIZE 100
void cleartoendofline(void);/*ANSI函数原型*/
void cleartoendofline(void){
而('\n'!=getchar());
}
main(){
char ch;/*处理用户输入分隔符*/
int num;/*用户输入编号*/
字符缓冲区[MAXBUFFERSIZE];/*足以处理一行*/
int char_count;/*此行读取的字符数*/
int exit_标志=0;
int有效_选择;
while(exit_标志==0){
printf(“输入整数)\n:”;
scanf(“%d%c”,&num,&ch);//读取整数和分隔符
字符计数=0;
而(char_count
了解
scanf
getchar()
之间的区别,尤其是当它们以相同的输入呈现时

非常非常仔细地阅读scanf的文档,其中有很多内容


通过编写自己的专用例程而不是调用
scanf()
,您可能会学到最多。复制C标准中的功能通常是不好的做法,但如果目标是帮助您学习,那也没关系。

好的,谢谢Eric,我不知道家庭作业标签“太好了”。那么,什么“不起作用”?怎么做?是SEGV吗?它不停止要求输入吗?它是否给出了错误的值?只是还没有执行“任务”吗?也就是说:问题是什么?它只返回与输出相同的字符串。我已尝试使用%d无效您将字符串读入缓冲区,然后
printf(“%s\n”,buffer)。除了打印与读入相同的字符串之外,您还希望发生什么事情?您使用
scanf
期望得到一个整数,但读入一个字符。此外,当您将97转换为“a”或其他任何形式时,不要假设字符是连续的,就像在ASCII中一样,或者甚至使用ASCII。请查看EBCDIC以了解这方面的示例。