2013-02-13 104 views
1

我的程序從標準輸入中讀取幾個字符串。我想要這樣編碼:其中是打印00,其中是B打印01.這是我的代碼。我不知道我錯在哪裏。謝謝!C中的文本編碼

#include<stdio.h> 
    #include<conio.h> 
    #include<string.h> 
    int main(void) 
    { 
    char text[100]; 

    printf("enter text:"); 
    fgets(text,100,stdin); 
    int i,j; 
    unsigned int aux; 
    char a[100]; 
    char b[100]; 
    for(i=0;i<100;i++) 
    for(j=0;j<100;j++) 
    { 
         if(text[i]=='a'){ 
         aux=aux|0; 
         aux=aux<<2; 
         a[j-1]=aux; 
         a[j]='\0'; 
         } 
        if(text[i]=='b'){ 
        aux=aux|1; 
        aux=aux<<2; 
        b[j-1]=aux; 
        b[j]='\0'; 
        } 
        strcat(a,b) 
         } 
         printf("%s", a[j]); 
       getch();  
    return 0; 
} 

回答

2

printf ("%02d\n", toupper(text[i]) - 'A');



for (i = 0; i < strlen (text); i++) 
    sprintf (&a[i*3], "%02d ", toupper(text[i]) - 'A'); 

請注意,這僅適用於純文本字符串