2012-03-18 28 views
0

我試圖在輸入文件中讀取64位,然後對這些64位進行一些計算,問題是我需要將ascii文本轉換爲十六進制字符。我已經搜索過,但沒有任何發佈的答案似乎適用於我的情況。C從文件讀取X字節,如果需要填充

以下是我有:

int main(int argc, int * argv) 
{ 
    char buffer[9]; 
    FILE *f; 
    unsigned long long test; 

    if(f = fopen("input2.txt", "r")) 
    { 
    while(fread(buffer, 8, 1, f) != 0) //while not EOF read 8 bytes at a time 
    { 
     buffer[8] = '\0'; 
     test = strtoull(buffer, NULL, 16); //interpret as hex 
     printf("%llu\n", test); 
     printf("%s\n", buffer); 
    } 
    fclose(f); 
    } 
} 

對於這樣的輸入:

「測試字符串爲十六進制的轉換」

我得到的結果是這樣的:

0 
testing 
0 
string t 
0 
o hex co 
0 nversion 

我期望的地方:

74 65 73 74 69 6e 67 20 <- "testing" in hex 
testing 

73 74 72 69 6e 67 20 74 <- "string t" in hex 
string t 

6f 20 68 65 78 20 63 6f <- "o hex co" in hex 
o hex co 

6e 76 65 72 73 69 6f 6e <- "nversion" in hex 
nversion 

任何人都可以看到我失誤的地方嗎?

回答

1

strtoull將由字符串表示的數字轉換爲無符號的long long。你對這個函數的輸入(例如字符串「測試」)是沒有意義的,因爲它必須是一個數字。

printf("%llu\n", strtoull("123")); // prints 123 

爲了得到你想要的結果,你必須打印字符串像這樣的每一個字符:

for(int i=0; i<8; i++) 
     printf("%02X ", (unsigned char) buffer[i]); 
+0

哇,不知道我怎麼忽視的是,我怎麼可能去ASCII文本轉換爲那麼相應的十六進制字節? – 2012-03-18 17:48:13

+0

@Hunter:看到我的編輯 – thumbmunkeys 2012-03-18 17:49:36

+0

謝謝,這正是我遇到的事情。 – 2012-03-18 17:52:30

1

功能strtoull(含16)轉換十六進制字符串到數字,而不是ASCII字符來HEX字符串。

要打印十六進制形式的字符,你應該這樣做printf("%02x ",buffer[0]);

0

你可以嘗試做getchar() 8倍(getchar返回的每個值1字節= 8位),然後使用atoh或東西 - 我我甚至不知道atoh是否存在,但是除非有atoi,然後itoh ..或者編寫自己的函數來轉換它。

1

strtoull()將十六進制格式(例如0xFFAABBEE)的字符串轉換爲整數格式。

你真正需要的是一個字符串轉換爲十六進制字符串,這樣的功能:

char *strToHex(const char *input) 
{ 
    char *output = calloc(1, strlen(input) * 3 + 1); 

    char *o = output; 
    int i = 0; 

    for (; input[i] != '\0'; o += 3, i++) 
    { 
     sprintf(o, "%.2X ", input[i]); 
    } 

    // don't forget to free output! 
    return output; 
} 
1

考慮使用limits.h中也是如此。

我去一個有點過分但也許一些這適合:

編輯:
Ehrmf。也許BITS_ULL的定義更符合你的追求。
I.e.東西方向:

#define BITS_ULL (sizeof(unsigned long long) * CHAR_BIT) 
#define BYTE_ULL (sizeof(unsigned long long)) 

,然後讀BYTE_ULL字節,但化妝舒爾檢查的讀取的字節大小,而不是它是否爲-1後者可能是一個粉碎。我有點不確定你在讀取位上的「計算」是什麼意思。

您可以讀取BYTE_ULL字節,並通過緩衝區[0]的地址強制轉換爲無符號long long,或考慮字節順序的位移。或以前和字符指針排序字節。

另外請注意,我已經使用len而不是null終止/ C字符串。

哦,這很有趣:) - 我在學習,而這種黑客攻擊就是天堂。
]

#include <stdio.h> 
#include <limits.h> /* BITS */ 
#include <ctype.h> /* isprint() */ 

#define CHUNK_BITS 62 
#define CHUNK_CHAR (CHUNK_BITS/CHAR_BIT) 
#define HEX_WIDTH 2 

/* print len hex values of s, separate every sep byte with space, 
* but do not add trailing space. */ 
void prnt_cshex(const char *s, int len, int sep) 
{ 
    const unsigned char *p = (const unsigned char*)s; 
    int i; 

    for (i = 1; i <= len; ++p, ++i) 
     fprintf(stdout, 
       "%02x" 
       "%s", 
       *p, 
       (i < len && !((i)%sep) ? " " : "")); 
} 

/* Print len bytes of s, print dot if !isprint() */  
void prnt_csbytes(const char *s, int len) 
{ 
    int i = 0; 

    for (i = 0; i < len; ++s, ++i) 
     fprintf(stdout, 
       "%c", 
       (isprint(*s) ? *s : '.')); 
} 

/* Pass file as first argument, if none, use default "input.txt" */ 
int main(int argc, char *argv[]) 
{ 
    const char *fn = "input.txt"; 
    FILE *fh; 
    char buffer[CHUNK_CHAR]; 
    const char *p = &buffer[0]; 
    size_t k; 

    if (argc > 1) 
     fn = argv[1]; 

    if ((fh = fopen(fn, "rb")) == NULL) { 
     fprintf(stderr, " * Unable to open \"%s\"\n", fn); 
     goto fail_1; 
    } 

    fprintf(stdout, 
     "Processing \"%s\"\n" 
     "Chunks of %d bytes of %d bits = %d bits\n", 
     fn, 
     CHUNK_CHAR, CHAR_BIT, CHUNK_CHAR * CHAR_BIT); 

    if (CHUNK_BITS != CHUNK_CHAR * CHAR_BIT) { 
     fprintf(stdout, 
     "%d bits chunk requested. Won't fit, trunkated to\n" 
      "%d * %d = %d\n" 
     "%d bits short.\n\n", 
     CHUNK_BITS, 
     CHUNK_CHAR, CHAR_BIT, CHUNK_BITS/CHAR_BIT * CHAR_BIT, 
     CHUNK_BITS - CHUNK_CHAR * CHAR_BIT); 
    } 

    while ((k = fread(buffer, 1, CHUNK_CHAR, fh)) == CHUNK_CHAR) { 
     prnt_cshex(p, CHUNK_CHAR, HEX_WIDTH); /* Print as hex */ 
     printf(" "); 
     prnt_csbytes(p, CHUNK_CHAR);  /* Print as text */ 
     putchar('\n'); 
    } 

    if (!feof(fh)) { 
     fprintf(stderr, " * Never reached EOF;\n"); 
     goto fail_close; 
    } 

    /* If input file does not fit in to CHUNK, report this */  
    if (k > 0) { 
     printf("%d byte tail: '", k); 
     prnt_csbytes(p, k); 
     printf("'\n"); 
    } 

    fclose(fh); 

    return 0; 
fail_close: 
    fclose(fh); 
fail_1: 
    return 1; 
}