The number system is the system of naming or representing numbers. Number systems are the technique to represent numbers in the computer system architecture.
Number system conversion is the process of converting one number system to another. In other words it is the conversion of number of one base to number of another base. Example : Binary to Decimal, Octal to Hexadecimal etc.
There are four number systems:
Binary Number system
A Binary number system has only two digits i.e 0 and 1 by which all the digital system performs. The base of binary number system is 2. Machine language is written only using binary codes.
int binaryToDecimal(string n)
{
string num = n;
int dec_value = 0;
int base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num[i] == '1')
dec_value += base;
base = base * 2;
}
return dec_value;
}
int binaryToDecimal(string n)
{
string num = n;
int dec_value = 0;
int base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num[i] == '1')
dec_value += base;
base = base * 2;
}
decToOctal(dec_value);
}
int decToOctal(int n)
{
int octalNum[100];
int i = 0;
while (n != 0) {
octalNum[i] = n % 8;
n = n / 8;
i++;
}
for (int j = i - 1; j >= 0; j--){
printf("%d",octalNum[j]);
}
}
int BinaryToHexadecimal(binary)
{
int hexConstant[] = {0, 1, 10, 11, 100, 101, 110, 111, 1000,
1001, 1010, 1011, 1100, 1101, 1110, 1111};
long long binary, tempBinary;
char hex[20];
int index, i, digit;
tempBinary = binary;
index = 0;
while(tempBinary!=0)
{
digit = tempBinary % 10000;
for(i=0; i<16; i++)
{
if(hexConstant[i] == digit)
{
if(i<10)
{
hex[index] = (char)(i + 48);
}
else
{
hex[index] = (char)((i-10) + 65);
}
index++;
break;
}
}
tempBinary /= 10000;
}
hex[index] = '\0';
for(int i=strlen(hex)-1;i>=0;i--){
printf("%c",hex[i]);
}
}
Decimal Number system
The Decimal number system uses the values from 0 to 9. The base of decimal system is represented by 10.
int decToBinary(int n)
{
int binaryNum[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for (int j = i - 1; j >= 0; j--){
printf("%d",binaryNum[j]);
}
}
int decToOctal(int n)
{
int octalNum[100];
int i = 0;
while (n != 0) {
octalNum[i] = n % 8;
n = n / 8;
i++;
}
for (int j = i - 1; j >= 0; j--){
printf("%d",octalNum[j]);
}
}
void decToHexa(int n)
{
char hexaDeciNum[100];
int i = 0;
while (n != 0) {
int temp = 0;
temp = n % 16;
if (temp < 10) {
hexaDeciNum[i] = temp + 48;
i++;
}
else {
hexaDeciNum[i] = temp + 55;
i++;
}
n = n / 16;
}
for (int j = i - 1; j >= 0; j--)
printf("%d",hexaDeciNum[j]);
}
Hexadecimal Number system
A Hexadecimal number system has sixteen (16) alphanumeric values from 0 to 9 and A to F. Each digit of hexadecimal number system can be represented as 4 digits known as "Hex digit" or "Nibble". The base of hexadecimal number system is 16.
void hextobin(char hex[])
{
int i=0;
while(hex[i])
{
switch(hex[i])
{
case '0': printf("0000"); break;
case '1': printf("0001"); break;
case '2': printf("0010"); break;
case '3': printf("0011"); break;
case '4': printf("0100"); break;
case '5': printf("0101"); break;
case '6': printf("0110"); break;
case '7': printf("0111"); break;
case '8': printf("1000"); break;
case '9': printf("1000"); break;
case 'A': printf("1010"); break; case 'a': printf("1010"); break;
case 'B': printf("1011"); break; case 'b': printf("1011"); break;
case 'C': printf("1100"); break; case 'c': printf("1100"); break;
case 'D': printf("1101"); break; case 'd': printf("1101"); break;
case 'E': printf("1110"); break; case 'e': printf("1110"); break;
case 'F': printf("1111"); break; case 'f': printf("1111"); break;
}
i++;
}}
int hexadecimal_to_decimal(int x)
{
int decimal_number, remainder, count = 0;
while(x > 0)
{
remainder = x % 10;
decimal_number = decimal_number + remainder * pow(16, count);
x = x / 10;
count++;
}
decToBinary(decimal_number);
}
int decToBinary(int n)
{
int binaryNum[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for (int j = i - 1; j >= 0; j--){
printf("%d",binaryNum[j]);
}
}
int hexadecimal_to_decimal(int x)
{
int decimal_number, remainder, count = 0;
while(x > 0)
{
remainder = x % 10;
decimal_number = decimal_number + remainder * pow(16, count);
x = x / 10;
count++;
}
return decimal_number;
}
Octal Number system
Octal number system has 8 digits from 0 to 7. The base of octal number system is 8.
int octalToDecimal(int n)
{
int num = n;
int dec_value = 0;
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 8;
}
decToBinary(dec_value);
}
int decToBinary(int n)
{
int binaryNum[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for (int j = i - 1; j >= 0; j--){
printf("%d",binaryNum[j]);
}
}
int octalToDecimal(int n)
{
int num = n;
int dec_value = 0;
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 8;
}
return dec_value;
}
int octalToDecimal(int n)
{
int num = n;
int dec_value = 0;
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 8;
}
decToHexa(dec_value);
}
void decToHexa(int n)
{
char hexaDeciNum[100];
int i = 0;
while (n != 0) {
int temp = 0;
temp = n % 16;
if (temp < 10) {
hexaDeciNum[i] = temp + 48;
i++;
}
else {
hexaDeciNum[i] = temp + 55;
i++;
}
n = n / 16;
}
for (int j = i - 1; j >= 0; j--)
printf("%d",hexaDeciNum[j]);
}