Input: 12
2's complement representation is: 00000000000000000000000000001100
(1) Divide the input repeatedly by 10 and collect the remainder
of the division.
This step will generate the individual decimal digits as follows:
12 ÷ 10 = 1 with remainder 2
repeat: 1 ÷ 10 = 0 with remainder 1
done
(2) Map each digit remainder to the corresponding ASCII code with:
ASCII code = digit + 48
This step will generate the ASCII codes: 50 ('2') 49 ('1')
(3) Concatenate the ASCII codes into a string
starting from the last remainder to the first remainder
|
|
If the integer value is negative (< 0), we must:
|
Input: (sample) -12
2's complement representation is: 11111111111111111111111111110100
(0) If ( input < 0 )
{
Remember that input is negative;
input = -input; // This is the 2's compl negate operation
// Input is now: 00000000000000000000000000001100 (=12)
}
(1) Divide the input repeatedly by 10 and collect the remainder
of the division.
This step will generate the individual decimal digits as follows:
12 10 = 1 with remainder 2
repeat: 1 10 = 0 with remainder 1
done
(2) Map each digit remainder to the corresponding ASCII code with:
ASCII code = digit + 48
This step will generate the ASCII codes: 50 ('2') 49 ('1')
(3) Concatenate the ASCII codes into a string
starting from the last remainder to the first remainder
(4) If ( input was negative )
{
prepend the character '-' to the output;
}
|
The algorithm expressed in Java is as follows:
/* ---
myToString(): Integer to Ascii conversion function
--- */
public static String myToString(int input)
{
boolean inputWasNeg;
int remainder[] = new int[100];
char digit[] = new char[100]; // Max 100 digits in number
String result;
int nDigits;
/* ---------------------------------------
Check if input is negative
--------------------------------------- */
if (input < 0)
{
inputWasNeg = true;
input = -input; // Negate to make input positive
}
else
{
inputWasNeg = false;
}
/* -------------------------------------------------------
Get all digit by collecting remainders divided by 10
------------------------------------------------------- */
nDigits = 0; // Count # digits in number
while (input > 0)
{
remainder[nDigits] = input % 10;
nDigits++;
input = input / 10;
}
/* ---------------------------------------------------------
Convert "digits" to character (ASCII code) using:
ASCII code = digit + 48
--------------------------------------------------------- */
for (int i = 0; i < nDigits; i++)
{
digit[i] = (char) (remainder[i] + 48); // Or use '0' for 48
}
/* ---------------------------------------------------------
Make a string (start with last remainder)
--------------------------------------------------------- */
result = ""; // Initialize output string
for (int i = nDigits-1; i >= 0; i--)
result = result + digit[i]; // Add next digit
/* ---------------------------------------------------------
Prepend '-' if input was negative
--------------------------------------------------------- */
if ( inputWasNeg == true )
{
result = "-" + result; // Try: "******" + result for banks
}
else
{
result = result; // Does nothing...
}
return(result);
}
|
The algorithm is doing exactly what I describe with the example...
(1) In the
"Check if input is negative" code segment
we test input < 0
If so: we set inputWasNeg = true and negate the input
If not: we set inputWasNeg = false
(2) In the
"Get all digit by collecting remainders divided by 10"
we divide the input repeatedly by 10
The remainders of the division are stored in:
reminder[0] reminder[1] .... remainder[nDigits-1]
(3) In the
Convert "digits" to character (ASCII code)
we convert each reminder[i] to its digits using:
digit[i] = remainder[i] + 48
(4) In the
Make a string (start with last remainder)
we make this string:
"digit[nDigits-1] digit[..] .... digit[1] digit[0]"
(5) Finally, in the input was negative, we prepent a '-' character
to the result.
|
How to run the program:
|