2016-11-14 131 views
2

我是這個主題的新手。 我想將整數數組轉換爲字符串。然後將字符串轉換爲整型數組,以檢查是否獲得相同的輸入。將字符串轉換爲整數數組c

gint16 frame[5] = {10, 2, 3, 7, 5}; 
char *str = malloc(sizeof(char) * (sizeof(frame)+1)); 
char *strp = str; 
size_t j; 
for (j= 0; j < sizeof(frame); j++) { 
    snprintf(strp, 4, "%02x", frame[j]); //hexadecimal 
     strp++; 
} 
// from hexa string to 16 bit integer array 
gint16 n_oframe[5]; 
size_t i_m; 
for (i_m = 0; i_m < 5; i_m++) { 
    char *d = (char*)malloc(sizeof(gint16)); 
    strncpy(d,str,2); 
    n_oframe[i_m] = atol(d); 
    str = str + 2; 
    free(d); 
} 

當我嘗試打印出來n_oframe值,我得到的正確的結果。請幫我

+2

'sizeof(char)*(sizeof(frame)+1)'應該給出什麼?特別是考慮到「框架」的類型.. –

+0

該數組的* length *(元素的數量)是'sizeof frame/sizeof frame [0]'。 –

+0

'free(str);'會因爲你增加了str而崩潰。 – Karthick

回答

1

使用這些功能:

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

typedef int16_t gint16; // define gint16 if not compiling with glib.h 

char *gint16_to_string(gint16 *p, int n) { 
    char *str = malloc(n * 4 + 1); 
    if (str) { 
     for (int i = 0; i < n; i++) { 
      snprintf(str + i * 4, 5, "%04X", p[i] & 0xFFFF); 
     } 
    } 
    return str; 
} 

void string_to_gint16(gint16 *p, int n, const char *str) { 
    if (str) { 
     for (int i = 0; i < n; i++) { 
      unsigned int x = 0; 
      sscanf(str + i * 4, "%4x", &x); 
      p[i] = (gint16)x; 
     } 
    } 
} 

int main(void) { 
    gint16 frame[5] = { 10, 2, 3, 7, 5 }; 

    // encoding in hexadecimal 
    char *str = gint16_to_string(frame, 5); 
    printf("encoded string: %s\n", str); 

    // from hexa string to 16 bit integer array 
    gint16 n_oframe[5]; 
    string_to_gint16(n_oframe, 5, str); 

    printf("n_oframe: "); 
    for (int i = 0; i < 5; i++) { 
     printf("%d, ", n_oframe[i]); 
    } 
    printf("\n"); 

    free(str); 
    return 0; 
} 
+0

如果'gint16'是一個32位'int'系統上的帶符號類型,'gint16 frame [5] = {-256};'會打印'FFFF' - 不太可能是有意的。 OTOH,OP的文章缺乏這方面的考慮。建議'snprintf(str + i * 4,5,「%04X」,p [i]&0xFFFFu);' – chux

+1

@chux:很好的一點,我雖然使用'%04hX',但'short'可能大於16位,答案更新。 – chqrlie

1

的評論者發現,大多數的,所以把它放在一起

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

// ALL CHECKS OMMITTED! 

int main() 
{ 
    int16_t frame[5] = { 10, 2, 3, 7, 5 }; 
    // hexadecimal string = 2 characters plus NUL 
    // sizeof(char) == 1, if compiler is standard compliant 
    char *str = malloc(3 * (sizeof(frame)/sizeof(frame[0]) +1)); 
    char *strp = str; 
    size_t j; 
    for (j = 0; j < sizeof(frame)/sizeof(frame[0]); j++) { 
    // again: hexadecimal string = 2 characters plus NUL 
    snprintf(strp, 3, "%02x", frame[j]);  //hexadecimal 
    strp += 2; 
    } 
    // we need a pointer to the start of the string to free it later 
    strp = str; 
    // let's see if we gott all of them 
    printf("str = %s\n",str); 
    // from hexa string to 16 bit integer array 
    int16_t n_oframe[5]; 
    size_t i_m; 
    // and again: hexadecimal string = 2 characters plus NUL 
    // a simple char d[3]; would have been more than suffcient 
    // for the task, but if stack is precious... 
    char *d = (char *) malloc(3); 
    for (i_m = 0; i_m < 5; i_m++) { 
    // it's always the same, just do it once at the beginning 
    //char *d = (char *) malloc(3); 
    strncpy(d, str, 2); 
    // atol() is for base 10 input only, use strtol() instead 
    n_oframe[i_m] = (int16_t)strtol(d,NULL,16); 
    str = str + 2; 
    //free(d); 
    } 
    for (j = 0; j < 5; j++) { 
    printf("%d ", n_oframe[j]); 
    } 
    putchar('\n'); 

    free(d); 
    free(strp); 
    exit(EXIT_SUCCESS); 
} 

我改變gint16int16_t,因爲我不知道那是什麼應該是。你很可能用gint16代替它,沒有問題。

+0

用'snprintf(strp,3,「%02x」,0xFFu&frame [j])來保證輸出有限;'。一個'int16_t'可以有像4096或-1這樣的值,它會超出分配的緩衝區。 – chux

+0

@chux'snprintf'自動處理它。 'int16_t frame [5] = {10,4096,-1,7,5}'將產生字符串'0a10ff0705'(10,16,255,7,5)。如果有足夠的空間可用,'snprintf'返回它將會打印的字符數。你需要檢查,但寫在代碼的頂部:所有檢查ommitted! (包括拼寫檢查)。 – deamentiaemundi

+0

的確,我讀到'sprintf()'。 – chux