What does the conversion algorithm do:
Input: An ASCII number string
Example: "-12"
00101101 00110001 00110010 (ASCII codes)
Output: The 2s complement code
for the input number string
Output for the example:
11111111111111111111111111110100
|
The conversion algorithm in pseudo code:
Sample Input: s = "-12" 00101101 00110001 00110010 (1) pos = 0; // Default starting position to process input If (s.charAt(0) == '-') { Remember negative sign pos = 1; // Start processing at char pos 1 } 00110001 00110010 |
The conversion algorithm in pseudo code:
Sample Input: s = "-12" 00101101 00110001 00110010 00110001 00110010 (2) Map each ASCII code to it's 2s complement equivalent 00000001 00000010 |
The conversion algorithm in pseudo code:
Sample Input: s = "-12" 00101101 00110001 00110010 00110001 00110010 00000001 00000010 (3) Compute the value (in binary) using formula ∑dk×10k: 00000001×000010101 + 00000010×000010100 = 00001100 Problem: how to find the exponent ? |
(I used 8 bits 2s complement representation, the Java program will use 32 bits (int) representation)
This is how to find the multiplication factor (exponent) for a digit in the number string:
Example: charAt(k=0) | V Input number: 2 5 6 7 (length = 4 digits) Factor: 103 102 101 100 -------------------> charAt(k) Loop variable k = 0 1 2 3 Exponent = 3 2 1 0 Exponent (= Position) = (length - 1) - k |
The conversion algorithm in pseudo code:
Sample Input: s = "-12" 00101101 00110001 00110010 00110001 00110010 00000001 00000010 00001100 (4) If (number was negative) { result = -result; } 11110100 (<--- -12(10)) |
(I used 8 bits 2s complement representation, the Java program will use 32 bits (int) representation)
/* ----------------------------------------------------- Power(k): Return 10^k in Binary 2's complement code ------------------------------------------------------ */ public static int Power(int k) { int r = 1; // Stores: 00000000000000000000000000000001 in variable r for (int i = 0; i < k; i++ ) { r = r * 10; // The Java compiler will convert 10 into the // binary number 00000000000000000000000000001010 // This statement will multiply bin number r // with 00000000000000000000000000001010 } return r; // Returns a BINARY number in 2's compl // that represents the VALUE 10^k } /* -------------------------------------------------------------- parseInt(s): Ascii to Integer conversion function Input: String (ASCII) representation of a number Output: int (2's complement) representation of same number -------------------------------------------------------------- */ public static int myParseInt(String s) { int[] digit = new int[20]; // Used to store individual digits in String s int value, sign; int i, pos; int len; /* ------------------------- Check for negative sign ------------------------- */ if (s.charAt(0) == '-') { sign = -1; pos = 1; // Start processing at charAt(1) } else { sign = 0; pos = 0; // Start processing at charAt(0) } /* ------------------------------------------- Convert each character digit into 2s compl (and keep a count on the number of digits ------------------------------------------- */ len = 0; // len counts # digits (without the leading '-') for ( i = pos; i < s.length(); i++ ) // Convert ASCII digits { digit[len] = s.charAt(i) - 48; len++; // Count # digits in number } /* ------------------------------------------ Compute the absolute value of the number ------------------------------------------ */ value = 0; for (int k = 0; k < len; k++) { /* --------------------------------------------------------------- The weight of the digits are: digit[0] ... digit[k] ... digit[len-2] digit[len-1] 10^((len-1)-k) 10^1 10^0 ---------------------------------------------------------------- */ pos = (len - 1) - k; value = value + digit[k]*Power( pos ); // High school knowledge.... } /* ======================================================== Negate 2's complement representation if first character of input was '-' ========================================================= */ if (sign == -1) value = -value; // Compute the negative value (= flip bits and add 1 !!) return(value); // Return a BINARY 2's compl code }
DEMO: /home/cs255001/demo/atoi/Atoi.java
There is a more elegant (but harder to understand) conversion algorithm:
public static int myParseInt(String s)
{
int value, sign;
int pos;
/* -------
Check for negative sign
------- */
if (s.charAt(0) == '-')
{ sign = -1;
pos = 1; // Start at digit s.charAt(1)
}
else
{ sign = 1;
pos = 0; // Start at digit s.charAt(0)
}
/* ------------------------------------------
Compute the absolute value of the number
------------------------------------------ */
value = 0;
for (int k = pos; k < s.length(); k++)
{
value = 10*value + ( s.charAt(k) - 48 );
}
/* ========================================================
Return the 2s complement representation
========================================================= */
return (sign*value); // If sign == -1, we will return
// the 2s complement negative value !
}
|