2016-12-09 22 views
1

我只學習C,我試圖循環一個字符串數組,然後轉換爲一個字節數組使用sscanf。使用Visual Studio的雖然循環了一串字符串,然後轉換爲一個字節使用sscanf_s c

#define BYTE unsigned char 

char stringarray[][3] 
{ 
    "98D9C2327F1BF03", 
    "98D9EC2327F1BF03", 
    "98D9EC2327F1BF03", 
} 

int main() 
{ 
    size_t i = 0; 
    for (i = 0; i < sizeof(stringarray)/sizeof(stringarray[0]); i++) 
    { 
     char hexstring[] = stringarray[i], *position[] = hexstring; 
     BYTE HexByteArray[8]; 
     size_t count= 0; 

     for (count = 0; count < sizeof(HexByteArray)/sizeof(HexByteArray[0]); count++) { 
     sscanf(position, "%2hhx", &HexByteArray[count]); 
     position += 2; 

     }  
    } 

    return 0; 
} 

錯誤2013

initializing' : cannot convert from 'char [3]' to 'char [] 
initialization with '{...}' expected for aggregate object 

回答

0

未經測試(但它是基於this),但它應該足以讓你開始:

#include <stdio.h> // sscanf 
#include <string.h> // strcpy 

#define BYTE unsigned char 

// you want 3 strings of a length at least 17 (+1 for null terminator) 
char stringarray[3][17] 
{ 
    "98D9C2327F1BF03", 
    "98D9EC2327F1BF03", 
    "98D9EC2327F1BF03", 
}; 

int main() 
{ 
    size_t i = 0; 
    for (i = 0; i < sizeof(stringarray)/sizeof(stringarray[0]); i++) 
    { 
     char hexstring[17]; 
     // copy stringarray[i] to hexstring 
     strcpy(hexstring, stringarray[i]); 
     // use a pointer 
     char *position = hexstring; 
     BYTE HexByteArray[8]; 
     size_t count= 0; 

     for (count = 0; count < sizeof(HexByteArray)/sizeof(HexByteArray[0]); count++) { 
      sscanf(position, "%2hhx", &HexByteArray[count]); 
      position += 2; 
     } 
     printf("%u\n", HexByteArray[0]); 
    } 
    return 0; 
} 

是你想要的在處理中的字符串時使用,請檢查它! ;)

相關問題