Hexadecimal is used in computers for representing numbers for human consumption, having uses for things such as memory addresses and error codes. Hexadecimal is used as it is shorthand for binary and easier for people to remember. The hexadecimal digits are the ordinary, base digits '0' through '9' plus the letters 'A' through 'F'. In the hexadecimal system, these digits represent the values 0 through 15, respectively.

Write a function named hexValue that uses a switch statement to find the hexadecimal value of a given character. The character is a parameter to the function, and its hexadecimal value is the return value of the function. You should count lower case letters 'a' through 'f' as having the same value as the corresponding upper case letters.

## Chet_kleitz_digelec_6|Arithmetic Operations and Circuits|Multiple Choice

If the parameter is not one of the legal hexadecimal digits, return -1 as the value of the function. If str is a string containing a hexadecimal integer, then the corresponding base integer can be computed as follows: Write a program that reads a string from the user.

If all the characters in the string are hexadecimal digits, print out the corresponding base value. If not, print out an error message. Discussion The subroutine has a parameter of type char and a return value of type int. It's easy to write the switch statement, although it's tedious because of the number of cases.

A little creative cut-and-paste can help. The switch statement has a default case that covers all the characters that are not hexadecimal digits. For such characters, a value of -1 is returned.

## Advantages of Octal Number Systems

The subroutine is shown in the program below, and I will not repeat it here. In the main program, I will use TextIO. This has the advantage that it will return a non-empty string that is guaranteed not to contain any blanks.

We still have the problem of checking whether the user's input contains only valid hexadecimal digits. One approach is to check all the characters first and use a boolean variable to record whether they are all valid.

Let hex be a string holding the user's input. Input is not a hexadecimal number. We can avoid this by checking the input at the same time that we do the conversion.

If the input is illegal, I want to end the program. I use the fact that a return statement in the main routine will end the program, since it returns control back to the system: Note that I declared dec to be of type long to allow bigger values than would fit in a variable of type int.

The program still has a problem if the user enters too many characters.A much more concise representation. in binary can be represented as F in hex. Binary Number Hexadecimal Number 3 2 D 9 The main advantage of a Hexadecimal Number is that it is very compact and by using a base of 16 means that the number of digits used to represent a given number is usually less than in binary or decimal.

Numbers can be written in three bases (decimal, octal, and hexadecimal). Although the programmer can choose to specify numbers in these three bases, once loaded into the computer, the all numbers are stored and processed as unsigned or signed binary.

One convenient way of representing binary numbers is using hexadecimal. Historically computers were programmed in assembly language, where code was written using processor instructions and meta-statements (known variously as directives, pseudo-instructions and pseudo-ops), comments and data.

Octal (base 8) and hexadecimal (base 16) are simply a "shortcut" for representing binary numbers, as both of these bases are powers of 2.

## Why We Should Switch to Hexadecimal

3 octal digits = 2 hex digits = 8 binary digits = 1 byte. It's easier for the human programmer to represent a bit integer, often . Hex numbers are very easy to be changed to binary. Each hexadecimal digit represents four binary digits (bits), and the primary use of hexadecimal notation is a human-friendly representation of binary-coded values in computing and digital electronics. 