2017-05-08 48 views
0

所以我正在開展一個實習項目。如何在不使用庫的情況下將整數數組轉換爲字符串

我必須開發一個完整的系統,收集一些數據,然後發送到服務器。該系統由一個MSP430G2553微控制器組成,因此我使用純C進行編碼而無需任何庫,因此我無法使用sprintf或malloc功能。

我現在遇到的問題是,我不知道如何將數組中的所有數據一次發送到服務器。

我有陣列ADCvalue [20]與20個值,範圍從0-350

ADCvalue[20]= {10, 40, 50, 90, 100, 200, 300, 240, 260, 10, 40, 50, 90, 100, 200, 300, 240, 260, 300, 20}

現在我想用服務器的每個值之間的分隔符「,」一次性發送這些值。 像這樣:

10,40,50,90,100,200,300,240,260,10,40,50,90,100,200,300,240,260,300,20 

我怎樣才能得到陣列像這樣,通過一次發送呢?我已經有一個打印功能,可以發送例如:char property[] = "property"作爲一個字符串。

如果有人能幫我用一些僞代碼或一些算法讓我開始,那將是非常好的。

+1

爲什麼不使用json? – bigbounty

+0

我通過短信發送數據,所以我不能通過json實際發送數據,我想 –

+0

一些未經測試的代碼,你可能會爲此任務工作:'for(i = 0; i <20; i ++ ){sprintf(tempstr,「%d」,ADCvalue [i]);的strcat(sendval,tempstr);的strcat(sendval, 「」); } sendval [strlen(sendval)-1] ='\ 0';' –

回答

0

試試這個代碼

const char ansitable[10] = {'0','1','2','3','4','5','6','7','8','9'}; 

void procInt(char* buf, int* loc, unsigned short v) { 
    if (v == 0) { 
     buf[*loc] = '0'; 
     *loc++; 
     return; 
    } 
    unsigned short cv = v; 
    unsigned short r; 
    unsigned short divid = 1; 
    if (v>9) divid = 10; 
    if (v>99) divid = 100; 
    while (divid > 0) { 
     r = cv/divid; 
     buf[*loc] = ansitable[r]; 
     *loc++; 
     cv %= divid; 
     divid /= 10; 
    } 
} 

int MAX_SIZE_OF_CHUNCK = 4; 

void startWrite(unsigned short* input, int sizeinput) { 
    char* buffer = (char *)calloc(sizeinput * MAX_SIZE_OF_CHUNCK, sizeof(char)); 
    int loc = 0; 
    for (int i = 0; i < sizeinput; i++) { 
     procInt(buffer, &loc, input[i]); 
     if (i < sizeinput-1) { 
     buffer[loc] = ','; 
     loc++; 
     } 
    } 
    sendToMPU(buffer, loc); //loc - length 
} 
1

正如你指出不使用任​​何庫(無論何種原因),我只是寫了一個簡單writeIntValue - 功能。請參閱下面的代碼,它使用此函數將一個正整數值序列寫入字符數組。 希望它有幫助。

char* writeIntValue(int val, char* dest) { 
    // count digits: 
    int digits = 0; 
    int valCopy = val; 
    while (valCopy) { 
     digits++; 
     valCopy /= 10; 
    } 

    if (digits == 0) // means val has been 0 
     digits=1; // assume 1 digit to write the '0'-value 

    for (int i=digits-1; i>=0; i--) { 
     dest[i] = val%10 + '0'; 
     val/=10; 
    } 
    dest[digits] = '\0'; // write string-terminating 0x0-value 
    return dest + digits; // return a pointer to the end of the value written so far 
} 

int main() { 

    int ADCvalue[20]= {10, 40, 50, 90, 100, 200, 300, 240, 260, 10, 40, 50, 90, 100, 200, 300, 240, 260, 300, 20}; 

    char buffer[500] = { '\0' }; 
    char *dest = buffer; 
    for (int i=0; i<20; i++) { 
     if (i>0) 
      *dest++ = ','; 
     dest = writeIntValue(ADCvalue[i], dest); 
    } 
    // buffer here will be "10,40,50,90,100,200,300,240,260,10,40,50,90,100,200,300,240,260,300,20" 
    return 0; 
} 
0

我怎樣才能得到這樣的陣列,通過一次發送呢?我 已經有一個打印功能,可以發送例如:char property [] =「property」作爲一個字符串。

範圍[0 ... 350]中的值不適合單個字節,但可以使用字節對編碼這些值。

一個例子

short int x = 300; /* 0x012C */ 

=

unsigned char arr[] = {0x01, 0x2C}; 

short int x[] = {0x000a, 0x0028, 0x0032, 0x005a, 0x0064, 0x00c8, 0x012c, 0x00f0, 0x0104, 0x000a, 0x0028, 0x0032, 0x005a, 0x0064, 0x00c8, 0x012c, 0x00f0, 0x0104, 0x012c, 0x0014}; 

=

unsigned char arr[] = {0x00, 0x0a, 0x00, 0x28, 0x00, 0x32, 0x00, 0x5a, 0x00, 0x64, 0x00, 0xc8, 0x01, 0x2c, 0x00, 0xf0, 0x01, 0x04, 0x00, 0x0a, 0x00, 0x28, 0x00, 0x32, 0x00, 0x5a, 0x00, 0x64, 0x00, 0xc8, 0x01, 0x2c, 0x00, 0xf0, 0x01, 0x04, 0x01, 0x2c, 0x00, 0x14}; 

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

int main(void) 
{ 
    short int ADCvalue[] = {10, 40, 50, 90, 100, 200, 300, 240, 260, 10, 40, 50, 90, 100, 200, 300, 240, 260, 300, 20}; 
    size_t sz = sizeof(ADCvalue)/sizeof(*ADCvalue); 
    unsigned char *arr = malloc(sz * 2); 

    if (arr == NULL) { 
     perror("malloc"); 
     exit(EXIT_FAILURE); 
    } 
    /* Encode */ 
    for (size_t i = 0; i < sz; i++) { 
     arr[i * 2 + 0] = (unsigned char)(ADCvalue[i] >> 8); 
     arr[i * 2 + 1] = (unsigned char)(ADCvalue[i] & 0xFF); 
    } 
    /* Send array */ 
    /* .. */ 
    /* Decode */ 
    for (size_t i = 0; i < sz; i++) { 
     printf("%d\n", (arr[i * 2] << 8) | (arr[i * 2 + 1])); 
    } 
    return 0; 
} 
相關問題