Why Must We Convert Numeric Data to ASCII And How Do We Do
It?
First of all, you should know that when we are typing in
keystrokes, we are actually entering characters.
But, if we wish to store these keystrokes as integer values, we
must convert them.
*How do we do this?
First, lets see what characters can be converted.
á
the characters Ô0Õ to Ô9Õ
á
the characters Ô+Õ and Ô-Õ, but only if they
are the first characters in the string
*When converting, what must we consider?
We must consider two (2) things:
¥Whether
the character is legal
¥The
position of the character in the array
*Why must we consider the position?
Mainly because:
á
If the string = Ô6Õ, the integer value is: 6
á
If the string = Ô32Õ, the integer value is: 3*10 + 2 = 32
á
If the string = Ô675Õ, the integer value is: = 600 + 70 + 5 =
675
LetÕs take a look at the following c code:
void main()
{ char nstring[] = Ò724Ó; // the
character string to convert
int num = 0, // num will hold
the converted value
offset = 0; // array
index/offset
// check if the characters are legal
while ((nstring[offset] >= Ô0Õ) && (nstring[offset]
<= Ô9Õ))
// if yes, then convert AND set positional value
num = num*10 + nstring[offset++]
- Ô0Õ; }
Following the instructions through the loop:
offset |
Num |
nstring[offset]
|
condition
|
num=num*10
+nstring[offset++]-Õ0Õ
|
0 |
0 |
Ô7Õ = 55 |
True |
0*10 + 55 - 48 = 7 |
1 |
7 |
Ô2Õ = 50 |
true |
7*10 + 50 - 48 = 72 |
2 |
72 |
Ô4Õ = 52 |
true |
72*10 + 52 - 48 = 724 |
3 |
724 |
Ô\0Õ = 0 |
false |
** Loop Terminated |
*The only drawback to this program is that it does not take
into account the sign nor Ôwhite spacesÕ (spaces, CR & tabs)
Consider the following C Code:
int atoi(const char *stringnum); //
function prototype
void main()
{ int num;
char *nstring = "-82"; // letÕs check
a neg. no.
num = atoi(nstring); }
// call the function
int atoi(const char *stringnum)
{ int n = 0, // n will hold the
number
sign = 1; // if unsigned then
positive
while (*stringnum == ' ' || *stringnum
== '\n' || *stringnum == '\t')
stringnum++; // skip
the white spaces
if ((*stringnum == '+') || (*stringnum
== '-')) // if signed
{ if (*stringnum == '-') // negative
number ?
sign = -1; // then mark
it
stringnum++; } // and go to next
character
// the rest is the old procedure, BUT using
pointers
while ((*stringnum >= '0') && (*stringnum
<= '9')) // Legal value??
{ n = n * 10 + *stringnum -
'0'; // determine number to date
stringnum++; }
// go to next position
return(sign * n); } // return the
SIGNED value
*do we have to write this code each time we input
characters from the keyboard (OR from a file)?
YES and No ----
¥Each
time we get numeric data from the keyboard (or an ASCII file),
we MUST convert.
¥Because
the conversions are so common, there are readily available
library routines in
Function Name
Meaning Action
Atoi alpha to integer
Convert: string to
int
atoi alpha to long Convert: string to
long
atoi alpha to float convert: string
to float
*Do we need to convert integers (or longs, or floats)
to strings ?
Yes!--
Whenever we store numeric values to an ASCII file, we MUST
convert to a string
*How do we convert ?
ItÕs almost like converting decimal to binary. For instance: 87
10
2\87=1 1010111
2\43=1
2\21=1
2\10=0
2\5= 1
2\2= 0
2\1= 1
*What does this have to do with converting from
integers to string ?
Well, Instead of dividing by 2 and keeping track of the
remainder, we divide by 10 and keep track of the remainder
Consider the integer: Ò5409Ó
10\5409= 9 Convert to: Ô9Õ the only difference is
10\540= 0 convert to: Ô0Õ that we must first
10\54= 4 convert to: Ô4Õ convert the digit to a
10\5= 5 convert to: Ô5Õ character
0
collect from the bottom:
*How do we convert to a character ?
ItÕs Simple. just Add 48 (the character Ô0Õ) to the remainder.
*Consider the following C Code:
void main()
{ int decimal = 5409, // The integer we wish
to convert
idx = 0; // The
character array index
char bin[10]; // the character array for
out string
while (decimal > 0) // Continue until the
quotient is 0 (zero)
{ bin[idx++] = decimal % 10
+'0'; // store the remainder
decimal = decimal / 10;
} // get the new quotient
bin[idx] = '\0'; } // set in
the null character & decrement the index
This would execute as:
decimal |
idx
|
decimal > 0
|
decimal %10 + 48
|
decimal /10
|
bin
|
5409 |
0 |
true |
5409 % 10 +48 =57
|
5409/10 =540
|
Ò9Ó |
540 |
1 |
true |
540 % 10 + 48 = 48
|
540/10 =54 |
Ò90Ó |
54 |
2 |
true |
54 % 10 + 48 = 52 |
54/10 =5
|
Ò904Ó |
5 |
3 |
true |
5 % 10 + 48 = 53
|
5/10 =0 |
Ò9045Ó |
0 |
4 |
false |
|
|
Ò9045Ó
Ò9045\0Ó |
NOTE, however, that our string is: Ò9045\0Ó
--- We must reverse the order ---
If we were to ADD the c code:
int offset = 0; // we will start with the first
character
char temp; // for temporary storage
idx--; // DONÕT move Ô\0Õ (idx now
contains 3)
while (idx > offset)
{ temp = bin[idx]; // store
uppermost non-swapped character
bin[idx--]
= bin[offset]; // move lower char. to
upper & decrement
bin[offset++]
= temp; } } // move in the old uppermost
character
Following the loop:
idx |
offset |
bin |
Idx>offset |
temp |
Bin[idx—1]
|
Bin[
Offset
++] |
bin |
3 |
0 |
Ò9045\0Ó |
true |
Ô5Õ |
Ô9Õ |
Ô5Õ |
Ò5049\0Ó |
2 |
1 |
Ò5049\0Ó |
true |
Ô4Õ |
Ô0Õ |
Ô4Õ |
Ò5049\0Ó |
1 |
2 |
Ò5409\0Ó |
false |
|
***We are out of the loop
And the string is in the correct order!
*Must we write this code each time we write numeric
values to an ASCII file?
YES and No ----
¥Each
time we write numeric data to an ASCII, we MUST convert.
¥Because
the conversions are so common, there are readily available
library routines in
Function name meaning action
Itoa integer to alpha convert: int to
string
Ltoa long to alpha convert: long to
string
Ftoa float to alpha convert: float to
string
*And you are done!
Questions to ponderÉ
1)
what is a string?
2)
How are strings stored differently in ram than arrays?
3)
Explain what an atoi does.
4)
Explain what ftoa does.
1)
a string is actually a numeric array, but it only
requires 1 byte of storage per element.
2)
They require1 additional byte at the end of the array:
the NULL (Ô/0Õ) character.
3)
An atoi Converts a string to an integer.
4)
An ftoa Converts a real number (float) to a string.