People who use the computer are deald at the number of people at the time, in the computer, the number is expressed in binary, and our habit is ten
The number, so after the computer receives the decimal number here, the computer is translated, and the decimal number is converted to a binary number to process.
The process is automatically complete by the computer. But for programmers, sometimes the decimal number is converted to a binary number, and the number of hexadecions and eight enters
The number, or convert the hexadecimal number into a decimal number, this is not a relaxed job, in order to make this work easily happy, the author
Write a universal numerical converter TNS.c with Turbo C 2.0, using it easy to complete various numerics between 2 ~ 36
Change work.
First, the principle of digital conversion
The basic principle of the digital conversion is to remove a specified number, from the high to the low, one bit, and calculate the decimal value of each bit,
Take the specific power index of its number, draw the decimal value of this bit, add all the decimal values of all the number of decimers,
Then convert the decimal number to the number of specified numbers, this process can be performed using this decimal number, with this decimal number, with the designated
The number of groups is divided, and the remainder of the resulting amount is composed of the number of new numbers in the order of the one to ten, that is, the number of numbers is specified.
Second, the principle of programming
There are two key programs for programming:
(1) Convert the number of inputs into decimal number
Starting from the high position of the input value, each digit (x) is taken, it is determined whether it is a valid number of the digital (digital R), if it is a valid number, seeking
The decimal value is taken, then multiplied by N-1 power of several groups (R) (n is the number of digits to the left of the left) in the value of the number), that is, "x * r 攩 n-1 stir
", Find the decimal value represented by the number, and finally the decimal value of each number, that is, the decimal value of the value.
(2) Convert this number of decimers to the number of specified number
This conversion process can be easily implemented through the three functions provided by the C language, they can be represented by decimal.
Values (integer, long integer, unsigned long) converted to specified digital strings, and their usage is:
Char * ITOA (int value, char * string, int radix)
Cahr * LTOA (Long Value, Char * String, int RADIX)
Char * Ultoa (unsigned long value, char * string, int RADIX)
Among them, Value is a decimal number, and RADIX is a number of groups in the VALUE process, which must be between 2 to 36, string is a string.
Third, the program use method
This program uses a DOS command line format:
TNS
Among them, "Numerical" is the significant number of digital 1, "Number 1" and "Number 2" is a valid decimal number, which can take 2 to 36
Arbitrary number. E.g:
TNS 128 10 16
The result of the screen display is:
128 (10) = 80 (16)
Among them, the number of the equation is the number of inputs, the number of equations is output, the number in parentheses is the number of the number, such as the meaning of 80 (16) is hexadecimal
80, 128 (10) is a decimal 128.
Rethlean:
TNS 128 10 2
The screen is displayed as:
128 (10) = 10000000 (2) Fourth, source program list
/ ************************************************** ******* /
/ * Program Name: TNS.C 1.0 * /
/ * Author: Dong Zhanshan * /
/ * Completion date: 1995-11-09 * /
/ * Useful conversion between the various modifications between 2 ~ 36 * /
/ ************************************************** ******* /
#include
#include
#include
#include
/ * Display Help Information * /
void help ()
{
Printf ("/ Nusage: Translate The Number Between Two Number Systems / N");
Printf ("SYNTEX: TNS
exit (0);
}
/ * Display error message * /
Void Printerror (errno, num, base1)
CHAR Errno, * Num, * Base1;
{
Switch (errno) {
Case 1: Printf ("/ NERROR: Origin Number% S (% s) is valid !!! / n", num, base1);
Break;
Case 2: Printf ("/ NERROR: RADIX (% s) is invalid !!! / n% s / n", base1,
"Correct: RADIX> = 2 and Radix <= 36");
Break;
}
Help ();
}
/ * Digital conversion function * /
Void Transnum (NUM, BASE1, BASE2)
Char * Num, * Base1, * Base2;
{
INT I, K, L, M, J, IBASE1, IBASE2;
Long inum = 0;
CHAR TEMP [20];
Double r = 0;
i = strlen (num); / * Numerical length * /
IBASE1 = ATOI (Base1); / * Count 1 * /
IF ((ibase1 <2) || (ibase1> 36)) Printerror (2, ", base1); / * Is it effective? * /
IBase2 = ATOI (base2); / * Digital 2 * /
IF ((IBase2 <2) || (ibase2> 36)) Printerror (2, ", base2); / * Is it effective? * /
For (j = 0; j
R = POW (ibase1, i-j-1); / * Calculate the power index * /
IF (ibase1 <= 10) L = '9' - (10 - ibase1); / * Calculate effective scope * /
Else {
M = 'a' (IBASE1 - 11);
L = '9';
}
IF ((NUM [J]> = 48) && (NUM [J] <= L)) / * See the decimal value of each digit * / k = NUM [J] -48;
Else IF (ibase1> 10) {
/ * See the decimal value represented by each letter * /
IF ((NUM [J]> = 'A') && (NUM [J] <= m - 32))
K = NUM [J] - 'A' 10;
ELSE IF (NUM [J]> = 'A') && (NUM [J] <= m))
K = NUM [J] - 'A' 10;
Else Printerror (1, Num, Base1);
}
Else Printerror (1, Num, Base1);
Inum = k * (int) r; / * accumulated calculation results * /
}
/ * Output conversion result * /
Printf ("% s (% d) =% S (% D) / N", Num, IBASE1, LTOA (Inum, Temp, IBase2), IBase2);
}
/ * Main program * /
Main (Argc, Argv)
Int argc;
Char * argv [];
{
Static Char Num [10], Base1 [10], Base2 [10];
Printf ("(TNS) Translator of Number System 1.0 CopyRight (C) 1995 dong zhanshan / n");
Switch (argc) {
Case 1:
Case 2:
Case 3: Help ();
Break;
Case 4: STRCPY (NUM, Argv [1]);
STRCPY (Base1, Argv [2]);
STRCPY (Base2, Argv [3]);
Transnum (NUM, BASE1, BASE2);
}