STM32在SD卡上保存HEX号码
我使用此代码在SD卡上保存数据。它对角色有效,但是当我想保存十六进制数字时,它的输出是不可取的。
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);
}
当我使用%x时,它保存了“ 3”,但我需要“ 03”。
当我使用%c时,它将保存“ etx”,即“ 03”的ASCII形式,
该如何正确保存十六进制数字在SD卡上的TXT文件中!
I use this code to save data on sd card. 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".
when I use %c it saves "ETX" that is ascii form of "03"
how can I save hex numbers correctly in a txt file on sd card!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,如果长度为“ 1”,则写入文件中的“ my_data”数组。这意味着,您严格写一个字符。 “ 0x03”是4个字符。
其次,字符和价值不是相同的。含义,数字3与字符“ 3”不同。第3号是十六进制中的0x03或0x3(或者只要您知道它是十六进制的)或二进制中的0b0000000011。因此,当您将0x03写入文件中时,您会获得由0x03表示的ASCII字符。如果接收方中的某种软件可以读取该字符并将其施放给UINT8_T,则可以正确解释所有内容。您无需做任何事情,根本没有问题。它不是人类可读的0x03,因为在文件中,0x03被处理并显示为char,但是接收程序很容易将其识别为整数。有0x03值,这只是解释的问题。将那个字符视为UINT,您将获得您的价值。
另外,如果您实际上想将整个字符串“ 0x03”写入文件中(除非有充分的理由专门用于这种格式,那么它几乎是处理能力的浪费,在接收器端上施放传入的数据很容易),您将不得不手动组装此字符串(字符阵列)。您将必须将数字3转换为char“ 3”,手动将“ 0x0”附加到“ 3”,然后编写4个字符的数组。而且接收器仍将其视为一系列炭,您必须施放到十六进制号码。发射器侧的头痛很多,接收器侧的头痛很多。
只需像最初一样写“ 3”,并在接收器侧解释(铸造)为UINT8_T。
First of all, your "my_data" array that you write into the file if of length "1". Which means, you're writing strictly one character. "0x03" is 4 characters.
Second of all, character and value is not the same. Meaning, number 3 is not the same as character "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. So when you write 0x03 into file, you get ascii character represented by 0x03. 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. 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 value is there, it's just a matter of interpretation. Treat that char as uint, and you get your value.
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). You will have to convert number 3 into char "3", manually append "0x0" to "3" and then write an array of 4 chars. 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.
当数据为0x03时,我想在my_data中具有03;因此,我应该写入%02x,然后将两个数字(03)出现在my_data变量中。
我应该像这样定义my_data和sprintf:
when data is 0x03 I want to have 03 in my_data; so I should write %02x then two numbers (03) will come in my_data variable.
and I should define my_data and sprintf like this: