C++ Programming Code Examples
C++ > Conversions Code Examples
Convert Hexadecimal to Decimal
/* Convert Hexadecimal to Decimal
To convert a number from hexadecimal format into equivalent decimal format in C++ programming, you have to ask to the user to enter the hexadecimal number to convert it into decimal number to display the equivalent value in decimal format as shown here in the following program.
Following C++ program ask to the user to enter any number in hexadecimal format to convert it into decimal, then display the result on the screen: */
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
unsigned long convtodecnum(char hex[]);
void main()
{
clrscr();
unsigned long decnum;
char hex[9]; // 8 characters for 32-bit Hexadecimal Number and one for ' '
cout<<" Enter 32-bit Hexadecimal Number : ";
cin>>hex;
decnum = convtodecnum(hex);
cout<<"Value in Decimal Number is "<<decnum<<"\n";
getch();
}
unsigned long convtodecnum(char hex[])
{
char *hexstr;
int length = 0;
const int base = 16; // Base of Hexadecimal Number
unsigned long decnum = 0;
int i;
// Now Find the length of Hexadecimal Number
for (hexstr = hex; *hexstr != '\0'; hexstr++)
{
length++;
}
// Now Find Hexadecimal Number
hexstr = hex;
for (i = 0; *hexstr != '\0' && i < length; i++, hexstr++)
{
// Compare *hexstr with ASCII values
if (*hexstr >= 48 && *hexstr <= 57) // is *hexstr Between 0-9
{
decnum += (((int)(*hexstr)) - 48) * pow(base, length - i - 1);
}
else if ((*hexstr >= 65 && *hexstr <= 70)) // is *hexstr Between A-F
{
decnum += (((int)(*hexstr)) - 55) * pow(base, length - i - 1);
}
else if (*hexstr >= 97 && *hexstr <= 102) // is *hexstr Between a-f
{
decnum += (((int)(*hexstr)) - 87) * pow(base, length - i - 1);
}
else
{
cout<<"Invalid Hexadecimal Number \n";
}
}
return decnum;
}
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.
Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:
As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
Raise to power. The pow() function returns the result of the first argument raised to the power of the second argument. This function is defined in the cmath header file. pow() function is a library function of cmath header, it is used to find the raise to the power, it accepts two arguments and returns the first argument to the power of the second argument.
In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
The pointer is a C++ programming data type whose value refers directly to (or "points to") another value stored elsewhere in computer memory using its address. Pointer examples