2013-10-01 169 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; 
} 
相关问题