2013-10-01 104 views
1

我有一個文件,其中包含許多行和列的矩陣。它看起來像下面的東西:將字符串數組轉換爲十六進制數字C

fa ff 00 10 00 
ee ee 00 00 30 
dd d1 00 aa 00 

矩陣中的每個條目是一個八位數值的十六進制數。我想將這個文件讀入一個二維數組。

我有兩個問題:

  1. 使用在我的代碼的讀取方法,其*包含具有所述矩陣的每一條目(兩個字符)的陣列。我如何將每個條目傳遞給一個變量而不是兩個字符?

  2. 當我傳入單個變量,如何將其從字符轉換爲十六進制? 我的意思是「ff」應該轉換爲0xff。

我的部分代碼如下。如果可以使用更好的方法,我可以避免使用標記化函數。

char** tokens; 
char** it; 

while (fgets(line, sizeof(line), file) != NULL){ /* read a line */ 
    tokens = tokenize(line); // split line 

    for(it=tokens; it && *it; ++it){ 
     printf("%s\n", *it); 
     free(*it); 
    } // end for 
} // end while 

char** tokenize(const char* str){ 
    int count = 0; 
    int capacity = 10; 
    char** result = malloc(capacity*sizeof(*result)); 

    const char* e=str; 

    if (e) do { 
     const char* s=e; 
     e=strpbrk(s," "); 

     if (count >= capacity) 
      result = realloc(result, (capacity*=2)*sizeof(*result)); 

     result[count++] = e? strndup(s, e-s) : strdup(s); 
    } while (e && *(++e)); 

    if (count >= capacity) 
     result = realloc(result, (capacity+=1)*sizeof(*result)); 
    result[count++] = 0; 

    return result; 
} 
+0

你的意思是採取'FA FF 00 10 00'爲數字,那麼我們將得到'1078020018176'蘋果公司的執行? – prehistoricpenguin

+0

不,fa是單個條目。所以我會得到0xfa而不是「fa」。 – drdot

+0

請看下面我的帖子。 – prehistoricpenguin

回答

7

這裏是半答案:讀一個十六進制字符串,並把它變成一個值,你可以做到以下幾點:

#include <stdio.h> 

int main(void) { 
    char *myString="8e"; 
    int myVal; 
    sscanf(myString, "%x", &myVal); 
    printf("The value was read in: it is %0x in hex, or %d in decimal\n", myVal, myVal); 
} 

這可以讓你的答案「我怎麼讀兩個字符變成一個變量「,我希望你的問題的一部分。

至於下半年 - 如果你做了以下內容:

while (fgets(line, sizeof(line), file) != NULL){ /* read a line */ 
    tokens = tokenize(line); // split line 

    for(int ii=0; tokens[ii]; i++) { 
     printf("ii=%d; token = %s\n", ii, tokens[ii]); 
    } // end for 
} // end while 

,你會看到你的tokens陣列已經包含你所要求的內容:字符串。如果您在使用sscanf轉換他們每個人 - 比如像這樣:

int myValues[10]; 
for(int ii=0; ii<10; ii++) { 
    sscanf(tokens+ii, "%x", myValues+ii); 
    printf("The %dth converted value is %d - or %x in hex\n", ii, myValues[ii], myValues[ii]); 
} 

你可以看到,這樣做你想要的一切。我在上面使用了一個固定大小的數組 - 你清楚知道如何使用malloc來創建一個具有合適大小的動態分配數組(而不是固定值10)。

還有一點需要注意 - 數組元素myArray[0]的地址可以寫爲&myArray[0],或者簡單地寫爲myArray。對於其他元素,&myArray[5]myArray+5相同。指針數學的奇蹟之一,我希望這有助於。

+0

謝謝你的回答!將每個條目讀入單個變量是什麼? – drdot

+0

你是否知道每行有多少令牌,以及有多少行? – Floris

+0

是的。我知道這些數字,他們是從stdin傳入的。 – drdot

2

轉換爲十六進制數字是沒有意義的,因爲十六進制/十進制/二進制/ etc僅僅是一個值的表示形式,無論如何都以相同的方式存儲在內存中。

話雖這麼說,到十六進制字符串到它所表示的數字轉換一個簡單的方法是這樣的:

  1. 創建一個臨時緩衝區,你在前面加上"0x"到您的十六進制字符串(所以如果你有字符串"ff"使緩衝區包含"0xff")。
  2. 給緩衝區掃描功能(sscanf將適用於您的情況),並在格式字符串中給出正確的格式說明符(在這種情況下爲%x十六進制)。
  3. 從變量(您提供的地址爲sscanf)中檢索該值,然後按照您的喜好使用該值。
+0

謝謝。這個問題解決了。將每個矩陣條目讀入一個變量是什麼? – drdot

+0

只有'tokens [0]','token' [1]'怎麼樣? (除非你的意思是別的。) –

+0

解決了這個問題。你能在你的答案中加幾行,我會接受。但我仍然不明白爲什麼它[0]!= tokens [0]。 – drdot

0

你可能只是使用的scanfcin數據作爲數字閱讀,他們會自動採取空格或換行作爲分裂的性格,藉此例如:

int main() { 
    int val; 
    while (scanf("%x", &val) != EOF) 
    printf("val we get : %x\n", val); 

    return 0; 
} 

Here是測試。

0

結帳從hexString.c

static char byteMap[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; 
static int byteMapLen = sizeof(byteMap); 

/* utility function to convert hex character representation to their nibble (4 bit) values */ 
static uint8_t 
nibbleFromChar(char c) 
{ 
    if(c >= '0' && c <= '9') return c - '0'; 
    if(c >= 'a' && c <= 'f') return c - 'a' + 10; 
    if(c >= 'A' && c <= 'F') return c - 'A' + 10; 
    return 255; 
} 


/* Utility function to convert nibbles (4 bit values) into a hex character representation */ 
static char 
nibbleToChar(uint8_t nibble) 
{ 
    if(nibble < byteMapLen) return byteMap[nibble]; 
    return '*'; 
} 

/* Convert a buffer of binary values into a hex string representation */ 
char * bytesToHexString(uint8_t *bytes, size_t buflen) 
{ 
    char *retval; 
    int i; 

    retval = malloc(buflen*2 + 1); 
    for(i=0; i<buflen; i++) { 
     retval[i*2] = nibbleToChar(bytes[i] >> 4); 
     retval[i*2+1] = nibbleToChar(bytes[i] & 0x0f); 
    } 
    retval[i] = '\0'; 
    return retval; 
} 
相關問題