Getting Started

(lily) #1

Chapter 8: C Pointers and Arrays


fter this runs the initialbyte now contains only the ones integer from the original
t is: high = 5 and intialbyte = 4. We
ombine the high and low nibbles to get the converted byte.

This al


onverting a byte to the ASCII equivalent decimal character using BCD.

tes Tens and Ones and a third byte, Number, which we set to a
alue in the range of 0 to 99.

ollowing char integer is a
mple increment of the previous one. Meaning that adding the number 4 to the
racter ‘0’, which is 48, yields the ASCII character ‘4’,
8+4 = 52 which is the ASCII decimal value of the character ‘4’). So the

get
e ASCII characters for Number.

0x0F) + '0';

A

byte and high char contains the tens, tha
c


convertedbyte = (high << 4) | initialbyte;

gorithm is used in the CHAR2BCD2 function in the software.

C


We define two by
v


char Tens = 0;
char Ones = 0;
char Number = 54;


We use the character to BCD algorithm written as the function
CHAR2BCD2(char) in the software section below to convert the Number to the
BCD equivalent in Tens.


Tens = CHAR2BCD2(Number);


Now Tens has the BCD of the tens in the upper nibble and of the ones in the lower
nibble. We can convert this to an ASCII character for the integer by remembering
that the numerical value of ASCII ‘0’ is 48 and each f
si
value of the ASCII cha
(4
conversion of a decimal integer to its ASCII equivalent character is the simple
addition of 48 to the decimal integer.


Since the CHAR2BCD2 function loaded both the tens and ones parts of Number
into Tens, we need to extract the Ones and the Tens so that we can add 48 to
th


Ones = Tens;
Ones = (Ones &

Free download pdf