简体   繁体   English

将字符串转换为uint8_t的数组

[英]Convert string to array of uint8_t

For my project I need to read some single line text from a SD card, then get the hex or dec value of each character within the string and group those values in an array. 对于我的项目,我需要从SD卡读取一些单行文本,然后获取字符串中每个字符的十六进制或dec值并将这些值分组在一个数组中。

There are no whitespaces in the text and the lines end with \\n 文本中没有空格,并且行以\\ n结尾

I'm using this code to read all the content into a single string! 我正在使用此代码将所有内容读入单个字符串!

            String line = "";

            while (dataFile.available() != 0) 
            {   
              line = dataFile.readStringUntil('\n');    
              if (line == "") 
                break;  
            } 

For later use I need to calculate the hex values of each character, this code should iterate over the String and group it in an array. 为了以后使用,我需要计算每个字符的十六进制值,此代码应遍历String并将其分组为数组。

            int lineSize = line.length();

            uint8_t data[lineSize];

            for (int i = 0; i < lineSize; i++)
            {
              data[i] = line.charAt(i);                 
            } 

I really don't know wether this works or not, but I doubt that I will get the actual hex values... 我真的不知道这是否奏效,但我怀疑我是否会得到实际的十六进制值...

The values are somewhere but I really don't know how to access them! 这些值在某处,但我真的不知道如何访问它们!

The result should look like this: 结果应如下所示:

uint8_t data[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}

Think of Hexadecimal as just another format to display any data type (uint8_t, or char or int...) stored in memory. 十六进制只是显示存储在内存中的任何数据类型(uint8_t,char或int ...)的另一种格式。 In memory, its all binary, or hexadecimal. 在内存中,其全部为二进制或十六进制。 Just depends on how you want to look at it. 只取决于您要如何看待它。

For example: the following statements: 例如:以下语句:

long int    A = 34;
uint8_t     B = 34;
char        C = 34;
int         D = 34;

printf("0x%02x\n", 'A'); // surrounded with '' gives ASCII value of 65, then displayed in Hex
printf("0x%02x\n", A);
printf("0x%02x\n", B);
printf("0x%02x\n", C);
printf("0x%02x\n", D);

Results in: 结果是:
在此处输入图片说明

Breaking any string into its fundamental elements, (char, or uint8_t) and printing them as shown above will yield similar results for you. 将任何字符串分解成其基本元素(char或uint8_t)并按上面所示进行打印将为您产生类似的结果。

Edit : 编辑
For this input file (call it in.txt, in the executable directory): 对于此输入文件(在可执行目录中的in.txt中调用):

lkjahldfkjghlskjhlskjhlakdjgglsjkahlkj4hl5k6jh67=83kjhlkjshdf8f7s698s7dfgbslfkjbg   

And using this code: 并使用以下代码:

int main(void)
{
    FILE *fp;
    char filename[]=".\\in.txt";
    uint8_t c;
    int length=0, i=0;
    uint8_t *array;

    //Get number of entries in file:
    fp=fopen(filename, "r");
    c= fgetc(fp);
    while(c<255)
    {
        length++;   
        c= fgetc(fp);
    }
    fclose(fp);

    //give array sufficient space
    array = malloc(sizeof(uint8_t)*length);

    fp=fopen(filename, "r");

    //read file into array, and display as hexadecimal
    c = fgetc(fp);
    while(c<255)
    {
        array[i++]= c;
        printf("0x%02x\n", c);
        c = fgetc(fp);  
    }
    fclose(fp);
        getchar();//stop execution to view files (hit any key to exit)
        return 0
}  

You should see this output: (only first 20 or so values shown...) 您应该看到以下输出:(仅显示前20个左右的值...)
在此处输入图片说明

Strings in C/C++ are already arrays (even when abstracted by a higher level class like std::string ). C / C ++中的字符串已经是数组(即使被诸如std::string类的更高级别的类抽象时)。 The array elements are the character values of each character. 数组元素是每个字符的字符值。 Are you sure you can't just grab the string's . 您确定不能只抓住字符串的。 c_str () (or . data ()) and use that, possibly with a cast? c_str ()(或。 数据 ()),并使用该,可能与铸造?

Well, if by getting the hex code you mean getting the hex representation of those characters, you can do this - 好吧,如果通过获取十六进制代码表示要获取这些字符的十六进制表示形式,则可以执行以下操作-

const String hexDigits = "0123456789abcdef";
char hex[2] = "";

hex[0] = hexDigits[ (int)line.at(i) / 16 ];
hex[1] = hexDigits[ (int)line.at(i) % 16 ];

For example, if line.at(i) = A , then hex will be "41" . 例如,如果line.at(i) = A ,则hex将为"41"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM