将字符串转换成数组用C十六进制数 [英] Convert string array into hex numbers in C

查看:121
本文介绍了将字符串转换成数组用C十六进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多行和列的一个矩阵的文件。它看起来像下面的内容:

  FA FF 00 10 00
EE ee值00 00 30
DD D1 00 AA 00

在矩阵中的每个条目是一个八比特值的一个十六进制数。我想读取这个文件到一个二维数组。

我有两个问题:


  1. 使用在我的code中的读出方法,*它包含具有矩阵的各条目(两个字符)的阵列。我怎样才能通过每进入一个变量,而不是两个字符?


  2. 当我传递到单个变量,如何将它从字符转换为十六进制?
    我的意思是FF应转换为0xFF。


我的code部分如下。我可以避开记号化功能,如果更好的方法可以uesd。

 的char **标记;
焦炭**它;而(与fgets(行,sizeof的(线),文件)!= NULL){/ *读取一行* /
    令牌=记号化(线); //分割线    对于(IT =令牌,它和放大器;&放大器; *它;它++){
        的printf(%S \\ n,*吧);
        免费(*它);
    } //结束了
} //结束时焦炭**记号化(为const char *海峡){
    诠释计数= 0;
    INT容量= 10;
    焦炭**结果=的malloc(容量* sizeof的(*结果));    为const char * E = str中;    如果(E){做
        为const char * S = E;
        E = strpbrk(S,);        如果(计数> =容量)
            结果= realloc的(结果,(容量* = 2)* sizeof的(*结果));        结果[统计++] = E? strndup(S,E-S)的strdup(S);
    }而(E&安培;&放大器; *(++ e)条);    如果(计数> =容量)
        结果= realloc的(结果,(容量+ = 1)* sizeof的(*结果));
    结果[统计++] = 0;    返回结果;
}


解决方案

下面是半答案:读一个十六进制字符串,并把它变成一个值,你可以做到以下几点:

 的#include<&stdio.h中GT;诠释主要(无效){
  char *之的myString =8E;
  INT设为myVal;
  sscanf的(MyString的,%X,&安培;设为myVal);
  的printf(该值读取:这是%0x的十六进制或十进制\\ n%D设为myVal,设为myVal);
}

这让你回答了我怎么读两个字符为一个变量你的问题的一部分,我希望。

至于下半年 - 如果你做了以下内容:

 而(与fgets(行,sizeof的(线),文件)!= NULL){/ *读取一行* /
    令牌=记号化(线); //分割线    为(中间体二= 0;令牌[Ⅱ];我++){
        的printf(II =%d个;标记=%S \\ n,第二章,代币[II]);
    } //结束了
} //结束时

你会看到你的标记阵中已经包含你所要求的内容:字符串。如果您在使用转换他们每个人的的sscanf - 例如像这样:

  INT myValues​​ [10];
为(中间体二= 0;ⅱ小于10;ⅱ++){
  sscanf的(令牌+ II%X,myValues​​ + II);
  的printf(%的DTH转换值为%d - 或%进制\\ n X,II,myValues​​ [II],myValues​​ [II]);
}

您可以看到,这样做你想要的一切。我曾经在上面一个固定大小的数组 - 你清楚地知道如何使用的malloc 来做出正确的尺寸(而不是<$ C的固定值的动态分配的数组$ C> 10 )。

还要说明一点 - 一个数组元素的地址 myArray的[0] 可以写无论是作为&放大器; myArray的[0] ,或简称为 myArray的。对于其他元素,&放大器; myArray的[5] 相同 myArray的+ 5 。一个指针数学C中的奇迹,我希望这会有所帮助。

I have a file that contains a matrix of many rows and columns. It looks like something below:

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

Each entry in the matrix is a hex number of an eight bit value. I would like to read this file into a two dimensional array.

I have two problems:

  1. Using the read method in my code, *it contains an array that has each entry (two characters) of the matrix. How can I pass each entry into a single variable instead of two characters?

  2. When I pass into the single variable, how to convert it from character to hex? I mean "ff" should be converted to 0xff.

Part of my code is below. I can avoid the tokenize function if better methods can be uesd.

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;
}

解决方案

Here is half an answer: to read a hex string and turn it into a value, you can do the following:

#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);
}

This gets you the answer to the "how do I read two characters into one variable" part of your question, I hope.

As for the second half - if you did the following:

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

you would see that your tokens array already contains what you are asking for: the strings. If you convert each of them using sscanf - for example like so:

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]);
}

You can see that this does everything you want. I used a fixed size array in the above - you clearly know how to use malloc to make a dynamically allocated array of the right size (instead of a fixed value of 10).

One more note - the address of an array element myArray[0] can be written either as &myArray[0], or simply as myArray. For other elements, &myArray[5] is the same as myArray+5. One of the marvels of pointer math in C. I hope this helps.

这篇关于将字符串转换成数组用C十六进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆