Leader

Saturday 23 May 2015

Converting hexadecimal to binary

See also: Base conversion of positional number systems for other conversion functions.

Download
hex2bin2.m

Hexadecimal and binary
Hexidecimal and binary, like "normal" decimal are positional number systems, where individual digits have a value determined by their position in the sequence (see this post for a more detailed explanation). The difference between the three is that decimal has a base of 10 - it uses 10 numbers, 0-9, where hexadecimal has a base of 16 (using 0-9 and A, B, C, D, E and F) and binary has a base of 2 (using 0 and 1).

There are two ways to convert between hexadecimal and binary, mathematically, or by simply using a lookup table. Those interested in the mathematical process should check out the hex2dec2 and dec2bin2 functions (bin = dec2bin2(hex2dec2(hex))), which describe how to do each step. But if you'd just like to get it done and don't care about the maths behind it, just use hex2bin2. hex2bin2 uses the following lookup table to get the binary value of each hex digit.

Dec = Hex = Bin
 0   =   0   =   0000
 1   =   1   =   0001
 2   =   2   =   0010
 3   =   3   =   0011
 4   =   4   =   0100
 5   =   5   =   0101
 6   =   6   =   0110
 7   =   7   =   0111
 8   =   8   =   1000
 9   =   9   =   1001
10   =   A =   1010
11   =   B =   1011
12   =   C =   1100
13   =   D =   1101
14   =   E =   1110
15   =   F =   1111


Code run-through

function bin = hex2bin2(hex)

hex2bin2 takes one input (hex) and returns one output (bin). hex only needs to be a string if it contains letters, and can be entered with or without the 0x notion (ie. '0x23FC' or '23FC'). bin is returned as a string, to retain any leading zeros, but can be converted to a numerical value if needed with using str2double(bin).

hex = upper(num2str(hex));

hex2bin2 will treat hex as a string, so the first line converts it to a string if it isn't already using Matlab's num2str function. The function upper then converts any letters in hex to upper case, which will be useful later.

hex = strrep(hex, '0x', '');

Next the function strrep ("string replace") is used to replace the '0x' with '' (nothing) if it's present in hex.

hex = cellstr(hex');

cellstr is used to separate each digit in hex in to its own cell. hex is transposed first, so each digit is treated as a separate string by cellstr. Cell arrays are useful here, because the number of digits in each number will vary - hex is always 1 digit, whereas we'll need 4 digits of binary to represent the maximum single digit hex value (which is F, or 15 in decimal).

% Define lookup table
% (:,1)=dec, (:,2)=hex, (:,3)=bin
dhbT= {...
 '0', '0', '0000'; ...
 '1', '1', '0001'; ...
 '2', '2', '0010'; ...
 '3', '3', '0011'; ...
 '4', '4', '0100'; ...
 '5', '5', '0101'; ...
 '6', '6', '0110'; ...
 '7', '7', '0111'; ...
 '8', '8', '1000'; ...
 '9', '9', '1001'; ...
'10', 'A', '1010'; ...
'11', 'B', '1011'; ...
'12', 'C', '1100'; ...
'13', 'D', '1101'; ...
'14', 'E', '1110'; ...
'15', 'F', '1111' ...
};

The lookup table is defined within the function. This is useful for a number of reasons; it's available for reference, and means it doesn't need to be passed to the function each time. There's no real downside to doing this - it's not like the 15 lines of extra code will make the function too big to handle.

Also it provides a nice example of how to format code for readability. dhbT is a 15x3 cell array, and each row is written on its own row (note the commas between cells on each row, then a semi colon at the end of the row). The ... continues the command on the next line as if there is no line break following it (everything after the ... is treated as a comment). Overall, a much more readable block of code than: dhbT= {'0','0','0000'; '1','1','0001'; '2','2','0010'; '3','3','0011'; '4','4','0100'; '5','5','0101'; '6','6','0110'; '7','7','0111'; '8','8','1000'; '9','9','1001'; 10','A','1010'; '11','B','1011'; '12','C','1100'; '13','D','1101'; '14','E','1110'; '15','F','1111'};

for h = 1:length(hex)

To look up the binary value of each digit in hex, a for loop runs for each cell in hex (which at this point is a cell array with one digit in each cell).

    % Find matching row in hex column (2)
    row =  strcmp(hex{h}, dhbT(:,2));

First the hex value of the hex digit in hex{h} is found in the hex column (2) of look up table (dhbT). row is an index, which will have a 1 for the correct row.

    % Replace hex value with bin value
    hex{h} = dhbT{row, 3};
The index in row is then used to extract the binary digits from the binary column (3) of dhbT. These are then placed in hex{h}, replacing the hex value that was previously in that cell.

end

bin = cell2mat(hex');

Finally, hex is transposed to a cell array with one row (instead of one column), this means cell2mat will join all the cells back together into one string of 1s and 0s. Note that cell2mat leaves bin as a string, it doesn't convert it back to a numerical value. This isn't done here because there may be leading zeros. These will be dropped when converting back to a numerical value, so this is left for the user to do if they want to later.

No comments:

AdSense