Convert binary to 8-bit integer?

7 views (last 30 days)
Brandon
Brandon on 20 Mar 2012
I currently have the following code:
fid = fopen('input.txt', 'r');
l = fread(fid, 13,'uint8=>char');
N=0;
N(l=='a')=0;
N(l=='b')=10;
N(l=='c')=110;
N(l=='d')=111;
S = sprintf('%d', N);
F = num2str(reshape([S,zeros(1,8-rem(length(S),8))],[],8));
FF = bin2dec(F);
For demonstration purposes, let's say input.txt has a string of these characters: acacabcbaaacb
My code converts this string to binary (using the definitions in code), which ends up as: S = 011001100101101000011010
This works good, the trouble I have is I need to break these up into groups of 8 bits. F should be:
01100110
01011010
00011010
My current code, F =
000101
111000
110101
001010
Not sure how to fix that... Also, I need to convert that to decimal...if it were correctly grouped into 8 bits. I believe the bin2dec command will work...hopefully. I just can't get them grouped into 8 bits to do so. Please help.
I also need to then take this integer array created by this code, and decode it back to the letter string. If someone could help with code to convert the integer array back to binary, and then use the key:
a = 0
b = 10
c = 110
d = 111
To convert the binary back to acacabcbaaacb. Would I use some sort of while loop, or is there a simpler way? It must be done in order, from begining to end. The actual input.txt is 256000 characters long.
Thanks.

Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!