2013-05-09 229 views
-1
#include<stdio.h> 
#define msize 4096 
struct memory 
{ 
int a[msize]; 
}; 

void read(struct memory *m) 
{ 
int i; 
for(i=0;i<sizeof(msize);i++) 
{ 
    scanf("%d",&m->a[i]); 
} 
} 

int main() 
{ 
struct memory m; 
m.a[0]=250; // temperature value of 25,0 
m.a[4]=01; // heater status OFF 
m.a[8]=240; // temperature value of 24,0 
m.a[12]=00; // heater status ON 
m.a[16]=220; // temperature value of 22,0 
m.a[20]=00; // heater status ON 
read(&m); 
return 0; 
} 

我已經創建了一個特定內存大小的結構,並將一些值插入到數組中並從數組中讀取這些值。我很難將此值轉換爲ASCII。將數組中的值轉換爲ascii?

+0

看看itoa()方法 – Arun 2013-05-09 07:21:36

+0

「轉換爲ASCII」是什麼意思,你的意思是你只是想打印數組的值? – john 2013-05-09 07:30:18

+0

@john我試了一下,然後放棄了。 – 2013-05-09 07:43:57

回答

1

for(i=0;i<sizeof(msize);i++),它不會i<4096,而是會比較i爲整數的您platform.In 32位系統的大小,這將是4。所以循環剛4迭代後會退出。

此外在您的功能中,&m->a[i]將爲m->a[i],因爲m是一個指針。

PS也有一些是非常不對您program.You的邏輯main()被分配一些值數組元素,但覆蓋相同的功能read()。

至於將整數值從一個數組轉換爲ASCII,它相當簡單。您只需使用%c格式說明符,並通過數組元素作爲參數。但我必須指出,沒有450個ASCII值要打印。因爲一個字符只有一個字節,所以你可以用它來管理的最大長度是256個字符。

PS要打印的許多ASCII字符都是不可打印的。一個是其ASCII碼值等於0

+0

我明白了,但是如何將這個讀取值從數組轉換爲ASCII? – user2306769 2013-05-09 07:28:53

+0

@ user2306769首先使用一個'prinf()'來提示用戶輸入所需的值。如果你運行該程序,它將會打開並閃爍,並且用戶將不知所措。 – 2013-05-09 07:30:34

+0

我不希望用戶輸入任何值,但我正在通過讀取函數讀取值並將此讀取值轉換爲ASCII。 – user2306769 2013-05-09 07:37:05

0

我的建議是按字符輸入,如果你想以ASCII碼存儲。

這樣

#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 

#define msize 4096 
struct memory { 
    char a[msize]; 
}; 

/* 
m.a[0]=250; // temperature value of 25,0 
m.a[4]=01; // heater status OFF 
m.a[8]=240; // temperature value of 24,0 
m.a[12]=00; // heater status ON 
m.a[16]=220;// temperature value of 22,0 
m.a[20]=00; // heater status ON 
*/ 

int read(struct memory *m){ 
    char inputbuff[4+1]; 
    int i=0; 

    memset(m->a, ' ', sizeof(m->a)/sizeof(m->a[0]));//initialize by space 
    while(i<msize/(4+4)){ 
     printf("input temperature value e.g.25,0 : 250(and enter)\n"); 
     fgets(inputbuff, sizeof(inputbuff), stdin); 
     strncpy(m->a + i*4, inputbuff, 3); 
     //i+=1; 
     printf("input heater status ON or OFF e.g.ON : 00 , OFF : 01(and enter)\n"); 
     fgets(inputbuff, sizeof(inputbuff), stdin); 
     strncpy(m->a + (i+1)*4, inputbuff, 2); 
     i += 1; 
     printf("continue input? (y or n) : "); 
     fgets(inputbuff, sizeof(inputbuff), stdin); 
     if(*inputbuff=='n' || *inputbuff=='N'){ 
      break; 
     } 
    } 
    return i; 
} 

int main(void){ 
    struct memory m; 
    read(&m); 
    { 
     //check print 
     m.a[msize-1]='\0'; 
     printf("%s\n", m.a); 
    } 

    return 0; 
} 

添加更多

#include <stdio.h> 
#include <string.h> 

#define msize 4098 

int *storeInt(void *address){ 
    int *ip =(int*)address; 
    *ip = 777;//ip[0]=777;ip[1]=888;... 
    return ip; 
} 

char *storeChars(void *address){ 
    char *cp =(char*)address; 
    strcpy(cp, "hello"); 
    return cp; 
} 

void dump(void *address, size_t length){ 
    unsigned char *cp = (unsigned char*)address; 
    size_t i; 
    for(i=0;i<length;++i){ 
     if(i % 16 == 0) printf("\n"); 
     printf("%02X ", *cp++); 
    } 
} 

int main(void){ 
    static char memory[msize];//create a static memory of a particular size 
    char *block1, *block2; 

    block1 = &memory[0]; 
    block2 = &memory[1024*2];//split a virtual 

    int *ip; 
    ip=storeInt(block1);//store some values in the memory 
    printf("%d\n", *ip);//read the values from the memory 

    char *cp; 
    cp=storeChars(block2);//set hello\0 
    printf("%s\n", cp);//say hello 

    char ascValue[12]; 
    int value = *ip;//777 
    //convert "value of int" to Ascii 
    sprintf(ascValue, "%d", value); 
    //Display it 
    printf("value is %s\n", ascValue); 

    dump(block1, 8); 
    dump(block2, 8); 

    return 0; 
} 
+0

我想刪除我的答案,因爲我是唯一的答案。現在你也回答了,我會讓我的答案留下。 – 2013-05-09 09:06:06

+0

@Rüppell'sVulture我不確定他會做什麼。那些有很多好的答案的人。 – BLUEPIXY 2013-05-09 09:15:00

+0

我的任務是讀1)創建一個特定大小的靜態內存(是否有可能將內存分成塊?)2)在內存中存儲一​​些值3)從內存中讀取值4)將此值轉換爲Ascii 5)在終端中顯示它 – user2306769 2013-05-09 09:20:29

0

如果你的read()函數的目的是從內存中讀取並打印到終端,那麼你應該用printf()而不是scanf()。 printf()將通過%d格式字符串將整數值轉換爲ASCII。 scanf()從標準輸入讀取並存儲到內存。 scanf()通過%d格式字符串從ASCII轉換爲整數。

+0

#include #define msize 4096 int a [msize]; void write() { \t int i; (i = 0; i user2306769 2013-05-09 12:51:40