I have a .bin file(0's and 1's). I want to read 2 bits of information at a time from that file. How do I do it ?

조회 수: 2 (최근 30일)
Explanation of the problem :
Suppose I have the binary text as: " 010010001100101011001"
I want to read " 01 " first and then I want to read " 00 " and so on till I read all the data. How do I do it ?
My code:
fileID = fopen("newencryptedmsg.bin");
A = fread(fileID,[1,2],"uint8");
disp(A)
fclose(fileID);
I've written this code but it's giving me the wrong output.
Expeceted Output : 01
Output of my code : 49 48
Can you please help me with this. Thank you.
  댓글 수: 2
OKo
OKo 2022년 8월 9일
fileID = fopen("newencryptedmsg.bin");
A = fread(fileID,[1,2],"uint8") == '1';
disp(A)
fclose(fileID);
Your file is ASCII, so you have to compare your data with chars '1'/'0'
Vaibav Reddy
Vaibav Reddy 2022년 8월 9일
I tried your code but It was saying "Comparison between double and string is not supported".

댓글을 달려면 로그인하십시오.

답변 (2개)

Atsushi Ueno
Atsushi Ueno 2022년 8월 9일
편집: Atsushi Ueno 2022년 8월 9일
Reproduce the problem:
fileID = fopen('newencryptedmsg.bin','w');
fwrite(fileID,'010010001100101011001');
fclose(fileID);
type newencryptedmsg.bin % It's binary file, but it looks like ASCII file.
010010001100101011001
fileID = fopen("newencryptedmsg.bin");
A = fread(fileID,[1,2],"uint8");
disp(A)
48 49
fclose(fileID);
Correct the problem:
fileID = fopen('newencryptedmsg.bin','w');
fwrite(fileID,[0 1 0 0 1 0 0 0 1 1 0 0 1 0 1 0 1 1 0 0 1]');
fclose(fileID);
type newencryptedmsg.bin
Āā𐀁ā�
fileID = fopen("newencryptedmsg.bin");
A = fread(fileID,[1,2],"uint8");
disp(A)
0 1
fclose(fileID);
fileID = fopen("newencryptedmsg.bin");
A = fread(fileID,"uint8")' % just remove [1,2]
A = 1×21
0 1 0 0 1 0 0 0 1 1 0 0 1 0 1 0 1 1 0 0 1
fclose(fileID);
  댓글 수: 3
Vaibav Reddy
Vaibav Reddy 2022년 8월 9일
Ok when I'm trying to copy paste the text that was shown, it's not visible here. But what I can say it's some wierd looking text more like an encrypted text.

댓글을 달려면 로그인하십시오.


Atsushi Ueno
Atsushi Ueno 2022년 8월 9일
Thank you. Now I understand what you mean. I will revise my answer.
fileID = fopen('newencryptedmsg.bin','w');
fwrite(fileID,'Z'); % the contents is 0x5A (01011010b)
fclose(fileID);
fileID = fopen("newencryptedmsg.bin");
A = fread(fileID,2,'ubit1=>uint8');
disp(A)
0 1
fclose(fileID);

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by