Help me to store the values into a text file.
2 次查看(过去 30 天)
显示 更早的评论
These are the values i need to store as a text file .
29 C3 50 5F 57 14 20 F6 40 22 99 B3 1A 02 D7 3A
Kindly help me.
0 个评论
采纳的回答
Walter Roberson
2021-6-11
S = '29 C3 50 5F 57 14 20 F6 40 22 99 B3 1A 02 D7 3A'
Schar = char(sscanf(S, '%x', [1 inf]))
[fid, msg] = fopen('a text file.txt', 'wt');
if fid < 0
error('failed to open file for writing because: "%s"', msg);
end
fprintf(fid, '%s\n', Schar);
fclose(fid);
You should not be astonished if you can only read back 12 out of the 16 bytes of the result. You are using MS Windows, and in MS Windows, when you store as a TEXT file, the 0x1A (decimal 26) is the End Of File character. If you happened to have bytes that were 0A then you should expect that when you write as a TEXT file that those bytes would be converted to CR/LF pairs (0C 0A)
Which is to say that storing in TEXT files is a mistake when what you have to store is BINARY data.
3 个评论
Walter Roberson
2021-6-11
S = '29 C3 50 5F 57 14 20 F6 40 22 99 B3 1A 02 D7 3A'
Sbyte = uint8(sscanf(S, '%x', [1 inf]));
[fid, msg] = fopen('a sound file.wav', 'w');
if fid < 0
error('failed to open file for writing because: "%s"', msg);
end
fwrite(fid, Sbyte, 'uint8');
fclose(fid);
This assumes that the stream of bytes holds the entire wav file, and not just the sound data information. For example if the original file contained track information, then that would have had to have been transmitted as well in order for the above method to work.
If what was transmitted was just the sound data, then you need to tell us how the sound was encoded into binary. Most wav files (but not all) use 16 bit unsigned integers internally, but most people who read wav files in MATLAB receive double precision values instead, with a special 'native' option being required to receive the unsigned integers.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!