-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffmanBinaryToDecimal.m
42 lines (40 loc) · 1.22 KB
/
huffmanBinaryToDecimal.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function dec = huffmanBinaryToDecimal(binStr, varargin)
% HUFFMANBINARYTODECIMAL converts a binary representation of a number (as a
% char array of 1's and 0's) back to its decimal format.
% dec = HUFFMANBINARYTODECIMAL(binStr) converts the char array of 0's and
% 1's in binStr to its decimal representation in dec.
%
% dec = HUFFMANBINARYTODECIMAL(binStr, true) changes the output when
% binStr = '0':
% dec = HUFFMANBINARYTODECIMAL('0', true) produces a 0, whereas
% dec = HUFFMANBINARYTODECIMAL('0') produces a -1.
%
% For the rest of the values of binStr the second parameter does not make
% any difference.
%
% This is useful when decoding DC coefficients, since the Huffman table for
% DC values considers size = 0, whose output must be also 0, whereas for
% the rest of DC size values, and also for all the AC cases, binStr = '0'
% must be converted to dec = -1.
%
% See also HUFFMANDECIMALTOBINARY.
%
if isempty(varargin)
zerosize = false;
else
zerosize = varargin{1};
end
if (strcmp(binStr,'0'))
if (zerosize)
dec = 0;
else
dec = -1;
end
else
if (binStr(1) == '1')
dec = bin2dec(binStr);
else
dec = -1 * bin2dec(char(not(int32(binStr) - int32('0')) + '0'));
end
end
end