[Dspforum] bits2char and char2bits functions
Ryan Merton
lucidsmog at gmail.com
Fri Nov 14 10:11:37 MST 2008
I couldn't find these two functions anywhere on the web site. I went
ahead and wrote them myself, so I'm attaching them here for everyone
else's benefit:
function bitStream = char2bits(byteStream),
% Make unpacked binary vector out of input uint8s
bitSream = zeros(1, length(byteStream) * 8);
for n=1:length(byteStream),
bitStream((((n - 1) * 8) + 1):(n * 8)) =
double(dec2bin(uint8(byteStream(n)), 8) - 3*16);
end
function byteStream = bits2char(bitStream),
% Make '010101' string out of bitStream vector
asciiBitStream = char(bitStream + 3*16);
% Make uint8s out of bitStream
byteStream =uint8( zeros(1, ceil(length(bitStream)/8)));
for n=1:8:length(bitStream),
byteStream(floor(n / 8) + 1) = uint8(bin2dec(asciiBitStream(n:n+7)));
end
Thanks,
Ryan
More information about the Dspforum
mailing list