Leader

Showing posts with label Base conversion. Show all posts
Showing posts with label Base conversion. Show all posts

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

Converting hexadecimal to decimal

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

Download
hex2dec2.m

Hexadecimal numbers
"Normal" decimal numbers use a base 10 system, most likely because humans have 10 fingers. Hexidecimal is a base 16 number system (presumably because computer programmers have 16 fingers), so whereas the decimal number system has 10 numbers (0-9), hexadecimal has 16 (0-9, and A, B, C, D, E and F).

Like decimal (and binary) hexidecimal is a positional number system, where a numbers position determines its value. This means that calculating the value of a hexidecimal number is exactly the same process as calculating the value of a decimal number. Observe:

Consider the decimal value 2345. The value is obvious. It's 2345. But it can be broken down into the sum of 2000 + 300 + 40 + 5. And each of these values depends on the number of zeros, which is determined by position. Positions start from 0 and count up right to left, so the 2 in 2345 is in position 3, the 3 is in position 2, the 4 is in position 1 and the 5 in position 0. Mathematically this means the value of the 2 is 2*10^3 = 2000, of the 3 is 3*10^2 = 300, and so on. Overall the total value of the number is:

(2*10^3) + (3*10^2) + (4*10^1) + (5*10^0) = 2345.

For hexadecimal numbers, exactly the same process applies, except using a base of 16. The only caveat being that the letters, if there are any, need to be replaced with their equivalent decimal numbers first. So what's the value of the hex number 2345 (notice that for hex values without letters, it's not immediately obvious it's a hex number - so it should be written as either 0x2345 or 234516 to avoid this ambiguity).

0x2345 (hex) = (2*16^3) + (3*16^2) + (4*16^1) + (5*16^0) = 9026 (decimal).

If there are letters in the hex number, for example: 0xF34B, these need to be converted to decimal values first, which is simple: A=10, B=11, C=12, D=13, E=14, F=15. So,

0xF34B = (F*16^3) + (3*16^2) + (4*16^1) + (B*16^0) , or
0xF34B = (15*16^3) + (3*16^2) + (4*16^1) + (11*16^0) = 62283

This process is simple to implement in Matlab, and is very similar to the process to convert decimal values to binary (see dec2bin2). The only complication is the handling of the letters; in Matlab hexadecimal numbers must be strings, because they contain letters. This means that isn't not possible to directly perform mathematical operations on them, such as the above conversion, but Matlab has a number of convenient features for dealing with strings that we'll exploit.

Overall, this function will convert the hexadecimal input to decimal numbers, work out the value of each based on its position, the calculate the sum of all these values to give the final decimal value. It provides an alternative to Matlab's hex2dec function.

Tuesday, 2 December 2008

Converting decimal to binary and binary to decimal

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

Download
dec2bin2.m (decimal to binary)
bin2dec2.m (binary to decimal)

Final code
See below.

Explanation
MATLAB already contains two functions to do convert between binary and decimal (dec2bin and bin2dec) but I wanted to write my own as a learning exercise – both in scripting and to learn the actual conversion method. Looking at the code in MATLABs functions is pretty fruitless for the latter. There are a couple of difference between my scripts and MATLABs, for example my dec2bin2 function automatically buffers the output to 8 bits in the form of a vector rather than a string. Conversely my bin2dec string doesn’t require a string as an input, which is convenient for certain applications.

This code is written in a way to make the processes of conversion as clear as possible - it's all done with loops, which isn't technically the best way, but it serves a purpose here!


AdSense