Handout+2+ +What+Are+Hexadecimal+Numbers+ +MMB+ +v007

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Introduction to Hexadecimal Numbers

1. Writing all those 0’s and 1’s can take a lot of time and space. Remember what the value of count
looked like?

2. To make our lives easier, developers often use another number base besides decimal and binary –
base 16 or hexadecimal (often simply called “hex”).

In hexadecimal, we have 16 different numbers to use in counting. Since we only have 10 in


decimal, we have to add 6 new “numbers.” Universally, we use the letters A, B, C, D, E, and F as
the last 6 numbers.

Decimal Binary Hexadecimal


0 0 0
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
16 10000 10

3. Because both binary and hexadecimal are based on powers of 2, it is relatively easy to convert
between the two.

Copyright © 2012-2015 Page 1 of7


Valparaiso University
4. Our first example will be to convert a number from binary (10111010111B) to hexadecimal.

Begin by writing down your number from right to left while grouping the binary number into
groups of 4 digits. (Note, we have color coded the binary number to better illustrate this
procedure.)

10111010111B becomes 0111

1101 0111

101 1101 0111

5. Believe it or not, we are almost done. Now, look-up each group of four binary digits on the table
above in step 2.

101 1101 0111

5 D 7

The hexadecimal equivalent of 10111010111B is 5D7.

6. There are a couple of different way that you can indicate that a number is in hexadecimal:

a) Use a suffix of H: 5D7H


b) Use a prefix of 0x: 0x5D7
c) Use a suffix of 16: 5D716

Each of these are equally valid ways of denoting a hexadecimal number. However, in this course,
and in most programming languages, we will be using the second option.

As before, if we do not use any suffix, we (and CCS) will always interpret a number to be
decimal.

7. It is just as easy to convert from hexadecimal to binary. Let’s convert 0xE57A into binary.
Begin by writing down the hexadecimal number with its digits spaced out a little bit.

E 5 7 A

Copyright © 2012-2015 Page 2 of7


Valparaiso University
8. Now, look-up each group of hexadecimal digits on the table above in step 2 and write down their
binary equivalent:

E 5 7 A

1110 101 111 1010

9. Ok, we are almost done. Remember, each hexadecimal digit will have a four digit binary
equivalent. Here is the original table from step 2, but this time, we have added another column
that has the leading 0’s inserted in the binary column.

Decimal Binary Binary With Leading 0’s Hexadecimal


0 0 0000 0
1 1 0001 1
2 10 0010 2
3 11 0011 3
4 100 0100 4
5 101 0101 5
6 110 0110 6
7 111 0111 7
8 1000 1000 8
9 1001 1001 9
10 1010 1010 A
11 1011 1011 B
12 1100 1100 C
13 1101 1101 D
14 1110 1110 E
15 1111 1111 F
16 10000 10000 10

10. Now, look-up each group of hexadecimal digits on the table above in step 2 and write down their
binary equivalent:

E 5 7 A

1110 0101 0111 1010 0xE57A = 1110010101111010B

Copyright © 2012-2015 Page 3 of7


Valparaiso University
11. Because we often switch between hexadecimal and binary numbers, you may often see binary
numbers written in groups of four digits:

0xE57A = 1110010101111010B = 1110 0101 0111 1010B

Therefore, in the future, you should consider

1110010101111010B and 1110 0101 0111 1010B

to be equivalent.

12. The following program will let you see how CCS uses and represents hexadecimal numbers.
Create a new project called Hexadecimal in CCS and paste the program into main.c
(Instructions for creating projects can be found in the Section 1 handout, Let’s Get Started.

The program is identical to the one in the binary handout, but this time, the program can count to
the hexadecimal number 0xFFFF.

// Program to look at counting in hexadecimal

#include <msp430.h> // Used to make code easier to read

#define DEVELOPMENT 0x5A80 // Used to disable watchdog timer for development

main()
{
WDTCTL = DEVELOPMENT; // Disable watchdog timer for development

long count = 0; // Create variable named count and set equal to 0

while(count<0xFFFF) // Keep going until count is really big


{
count = count + 1; // Add 1 to variable count
}

while(1); // After counting, stay here forever

13. Save and Build your new program. Once the project is done building, go ahead and launch the
CCS Debugger.

Copyright © 2012-2015 Page 4 of7


Valparaiso University
14. Before single-stepping, change the Number Format of the count variable to Hex.

15. Now, you can keep clicking Step Into and watch CCS count up in hexadecimal.

16. Pay special attention as the value of count increments from 0x00000009 to 0x0000000A.

Copyright © 2012-2015 Page 5 of7


Valparaiso University
17. If at any time you make a mistake and want to restart the program and the counting process, click
the Soft Reset button.

This will effectively start your program over.

18. When you are ready, click the Terminate button to close the CCS Debugger and return to the
Editor.

Copyright © 2012-2015 Page 6 of7


Valparaiso University
All tutorials and software examples included herewith are intended solely for
educational purposes. The material is provided in an “as is” condition. Any
express or implied warranties, including, but not limited to the implied warranties
of merchantability and fitness for particular purposes are disclaimed.

The software examples are self-contained low-level programs that typically


demonstrate a single peripheral function or device feature in a highly concise
manner. Therefore, the code may rely on the device's power-on default register
values and settings such as the clock configuration and care must be taken when
combining code from several examples to avoid potential side effects.
Additionally, the tutorials and software examples should not be considered for use
in life support devices or systems or mission critical devices or systems.

In no event shall the owner or contributors to the tutorials and software be liable
for any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services; loss of
use, data, or profits; or business interruption) however caused and on any theory
of liability, whether in contract, strict liability, or tort (including negligence or
otherwise) arising in any way out of the use of this software, even if advised of
the possibility of such damage.

Copyright © 2012-2015 Page 7 of7


Valparaiso University

You might also like