Wednesday, 2 September 2015

Character to Number conversion algorithm


This blog explains about the algorithm which will explain about character to number conversion,

Given the character representation of an integer convert it to its conventional decimal format.
Algorithm development
            The difference between the character representation of an integer and its conventional number representation is to be known. The number of different characters needed to represent textual and string information is relatively small. The upper and lower case alphabetic characters, the ten digits, and various punctuation and control characters make up only about one hundred different characters.
In contrast to this, the range of different integers (and real numbers) that are needed may extend into the millions, the bil1ions and even further. To obtain unique representations for such a large span of numbers requires a considerable amount of "space" to store each individual number. Numbers are represented in the computer in binary positional notation using just the 0 and 1 digits. As an example, let us examine the binary and decimal positional representations for the decimal number 221.

Comparing the two representations for 221, only 3 decimal digits are needed to represent 221 whereas 8 binary digits are used. To represent one billion, we need 10 decimal digits and about 32 binary digits or bits. The fundamental unit or computer word size for storing numbers is usually one of 16,32,36,60 or 64 bits. since only 7 or 8 bits (8 bits are called a byte) are all that is needed to distinguish among the hundred or so characters that we are likely to need (i.e. 2M=256) it is therefore very wasteful of computer memory to store only one character in the basic storage unit for numbers.
The solution is therefore to pack several characters into each computer word. for example, on 32-bit computers we find that four 8-bit characters are usually stored in each computer word. To make this packing possible It has been necessary to assign fixed 8-bit codes to the one hundred or so characters. One of the most widely used of these coding systems is called the American Standard Code for Information Interchange or ascii code. Some examples from this system are shown in Table .
Note that the decimal digits ,'0, 1,2,3, ..., 9, a~ also assigned 8-bit character code values. Because of this it is convenient in many applications to represent numbers as sequences of character), For example, a date may be represented by a string of characters; for example,
                                                            23 April 1984
With representations like this there are times when it is necessary to do conventional arithmetic calculations on the numbers represented as sequences of 8-bit characters. In our example above the 23 does not have the value of 2 tens and 3 units but rather there are the decimal values.50 and 51 in successive8-bit bytes.
The arithmetic operations  cannot be done directly one character code representations because the corresponding binary representation does not conform to the standard binary positional notation used to represent numbers in the computer. . Our only alternative in such circumstances is to convert the number from its character representation to its conventional number internal representation. This brings us back to our original character-to-number conversion problem.
The task is therefore to accept as input a character representation of a number containing a known number of characters and make the conversion to the conventional decimal representation. To do this it will be necessary to use the knowledge of the particular character code being employed. In this example, we will choose to work with the ascii code. Fortunately, most programming languages including Pascal provide some additional functions to make tasks like this relatively straightforward to perform. To convert the four-character sequence, 1984 to the decimal number 1984. The representation start out with is,

To make the conversion, the 49 will need to be converted to 1000, the 57 to 900, the 56 to 80 and the 52 to 4 units. To get the appropriate decimal digit in each case we have had to subtract 48 (the ascii value of character 0) from the ascii value of each character. Pascal makes this job easier by providing a function called word which accepts an 8-bit character as its argument and returns as output its corresponding decimal (in our case ascii) value, e.g. the statement.
                                                            x := ord(‘9’)
will assign to x the decimal value 57.
In our example, to get the decimal digits subtract ord(‘0’) from the ascii values of each of the characters in the sequence.
The next step is to use the digits to construct the corresponding decimal number. The character sequence will need to be processed one character at a time to obtain the decimal digits. What we must now work out is how each digit is to be converted to make its appropriate contribution to the decimal number we are seeking.
Start processing the characters from the left, when " 1" is encountered it is not immediately obvious what power of ten it should be multiplied by after the ascii conversion.
            However, by the time when a second character is encountered in the sequence, it should be multiplied by at least 10. And, at the time we have encountered the third character, we know the second digit should have been multiplied by 10 and the first digit multiplied by 100. That is, for our example of 1984,


All that is left now is to work out the details of the mechanism for implementing this process. The "shifting-to-the-left" mechanism can be obtained at each step by multiplying the previous decimal value by 10 and adding in the current decimal digit. Details of our implementation can now be outlined. It is assumed that the length of the character string has been determined and that a check has been made to confirm that the string represents a positive integer.
Algorithm description
  1. Establish the character string for, conversion to decimal and its length n.
  2. Initialize decimal vaJue to zero.
  3.  Set base0 value to the ascii or ordinal value of '0'.
  4.  While less than n characters have been examined do
(a) convert next character to corresponding decimal digit,
            (b) shift current decimal value to the left one digit and add in digit for current character,
      5. Return decimal integer corresponding to input character representation.

No comments:

Post a Comment

Featured post

How to convert Java object to JSON or JSON to java object in java

Download Gson jar from this link  Quick Reference toJson() – Convert Java object to JSON Gson gson = new Gson ( ) ; <Java cla...