简体   繁体   English

stm32在sd卡上保存十六进制数

[英]stm32 save hex number on sd card

I use this code to save data on sd card.我使用此代码将数据保存在 SD 卡上。 it works good for character but when I want to save hex numbers it's output is not desirable.它对字符很有用,但是当我想保存十六进制数字时,它的输出是不可取的。

FATFS myfatfs;
FIL myfile;
UINT my_biytes;
char my_data[1];
// or uint_8
char myfilename;

/* USER CODE BEGIN 2 */

f_mount(&myfatfs, SDPath ,1)     
     
/* USER CODE END 2 */

  while (1)
  {
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */

          f_open (&myfile , "test2.txt" , FA_WRITE| FA_OPEN_ALWAYS  );
          data= 0x03;

          sprintf(my_data, "%x",data);
          // or  sprintf(my_data, "%c",data);

          f_lseek(&myfile, f_size(&myfile));
          f_write(&myfile, my_data, sizeof(my_data), &my_biytes);
          f_close(&myfile);

      }

when I use %x it saves "3" but i need "03".当我使用 %x 时,它会保存“3”,但我需要“03”。

when I use %c it saves "ETX" that is ascii form of "03"当我使用 %c 时,它会保存 "ETX",它是 "03" 的 ascii 形式

how can I save hex numbers correctly in a txt file on sd card!!如何将十六进制数字正确保存在 sd 卡上的 txt 文件中!

when data is 0x03 I want to have 03 in my_data;当数据为 0x03 时,我想在 my_data 中有 03; so I should write %02x then two numbers (03) will come in my_data variable.所以我应该写 %02x 然后两个数字(03)将进入 my_data 变量。 and I should define my_data and sprintf like this:我应该像这样定义 my_data 和 sprintf :

uint8_t my_data[2]

sprintf(my_data, "%02x",data);

First of all, your "my_data" array that you write into the file if of length "1".首先,如果长度为“1”,则写入文件的“my_data”数组。 Which means, you're writing strictly one character.这意味着,您正在严格地写一个字符。 "0x03" is 4 characters. “0x03”是 4 个字符。

Second of all, character and value is not the same.其次,性格和价值是不一样的。 Meaning, number 3 is not the same as character "3".意思是,数字 3 与字符“3”不同。 Number 3 is 0x03 or 0x3 in hex (or just 3 in hex, as long as you know it's hexadecimal) or 0b00000011 in binary.数字 3 是十六进制的 0x03 或 0x3(或者只是十六进制的 3,只要您知道它是十六进制)或二进制的 0b00000011。 So when you write 0x03 into file, you get ascii character represented by 0x03.因此,当您将 0x03 写入文件时,您将获得由 0x03 表示的 ascii 字符。 If some kind of software on the receiving side can read that character and simply cast it to uint8_t, it will interpret everything correctly.如果接收端的某种软件可以读取该字符并将其简单地转换为 uint8_t,它将正确解释所有内容。 You don't need to do anything, there is no problem at all.你不需要做任何事情,完全没有问题。 It's not a human-readable 0x03, because in the file that 0x03 is treated and displayed as if it's a char, but the receving program will easily recognize it as integer.它不是人类可读的 0x03,因为在文件中 0x03 被当作字符处理和显示,但接收程序很容易将其识别为整数。 0x03 value is there, it's just a matter of interpretation. 0x03 值在那里,这只是一个解释问题。 Treat that char as uint, and you get your value.将该 char 视为 uint,您将获得您的价值。

Alternatively, if you actually want to write the whole string "0x03" into the file (unless there is a good reason for specifically this format, it's pretty much a waste of processing power, casting incoming data on the receiver side is easier), you will have to manually assemble this string (array of chars).或者,如果您真的想将整个字符串“0x03”写入文件(除非有充分的理由专门使用这种格式,否则这几乎是浪费处理能力,在接收端转换传入数据更容易),您将不得不手动组装这个字符串(字符数组)。 You will have to convert number 3 into char "3", manually append "0x0" to "3" and then write an array of 4 chars.您必须将数字 3 转换为字符“3”,手动将“0x0”附加到“3”,然后写入一个由 4 个字符组成的数组。 And the receiver will still see it as an arrach of chars, that you will have to cast to hex number.并且接收者仍然会将其视为字符的一个字符,您必须将其转换为十六进制数字。 Much headache on the transmitter side, much headache on the receiver side.发射端很头疼,接收端也很头疼。

Just write "3" like you did initially and interpret (cast) it as uint8_t on the receiver side if it's possible.只需像最初那样写“3”,如果可能的话,在接收方将其解释(转换)为 uint8_t 。

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

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