2012-03-18 26 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; 
}