Skip to content Skip to sidebar Skip to footer

Read Files With Scanner With Hexadecimal Formatting

Hexadecimal in C 

Introduction to Hexadecimal in C

In C programming language, a hexadecimal number is a value having a made upwards of xvi symbols which have 10 standard numerical systems from 0 to 9 and half dozen extra symbols from A to F. In C, the hexadecimal number system is too known as base-16 number system. In C there is no data type to shop hexadecimal values similar bladder or long or double instead you can store in the integral blazon of data types. In C programming linguistic communication, hexadecimal value is represented as 0x or 0X and to input hexadecimal value using scanf which has format specifiers like %x or %X.

Functions of Hexadecimal in C Programming

In this article, we are discussing hexadecimal value in the C programming linguistic communication. Hexadecimal is likewise similar an integral value that has no separate data type. We already know that there are sixteen symbols for hexadecimal values similar 0, 1, ii, 3, four, five, 6, 7, 8, 9, A, B, C, D, E, F. Hither A, B, C, D, C, E, F represents 11, 12, 13, 14, fifteen. Let united states encounter an example and its syntax:

Syntax:

Scanf ("%10", &var_name);

To convert decimal number to hexadecimal number we have few steps to follow:

  • Firstly dissever the given decimal number by 16. (Consider it every bit integer division).
  • Note down the residuum of the above division of decimal numbers.
  • Then divide this residue by 16. Proceed until you get the result every bit 0. (Consider division equally integer division).
  • Then the hexadecimal value obtained volition be the sequence of digits of the remainder from last to first.

Examples of Hexadecimal in C

Permit usa try to convert a decimal number 590 to hexadecimal value using the above steps:

  • Separate 590 past 16 i.e. 590 / xvi result = 36 and residuum =14 ( E is hexadecimal value of 14).
  • Carve up the obtained result by 16 in the in a higher place step, so 36 / sixteen outcome = 2 and rest = 4 (4 is decimal value).
  • Carve up the obtained result by sixteen in the above pace, so 2 / 16 result = 0 and remainder =2 ( ii as decimal value).
  • So the hexadecimal value of decimal number 590 is the sequence of digits of the remainder from last to beginning which will be 24E.

Example #i

Now permit us encounter the program in C programming for converting decimal number to hexadecimal number:

Code:

#include<stdio.h>
int main() {
long int decNum,rem,quo;
int i=1,j,temp;
char hexadecNum[100];
printf("Enter whatsoever decimal number to catechumen it to hexadecimal Number: ");
scanf("%ld",&decNum);
quo = decNum;
while(quo!=0)
{
temp = quo % 16;
if( temp < 10)
temp =temp + 48; else
temp = temp + 55;
hexadecNum[i++]= temp;
quo = quo / 16;
}
printf("hexadecimal value of decimal number entered is %d: ",decNum);
for (j = i -i ;j> 0;j--)
printf("%c",hexadecNum[j]);
return 0;
}

Output:

Hexadecimal in C Example 1

In the above example, nosotros are printing decimal number 590 to its hexadecimal number as 24E.

Nosotros can fifty-fifty convert hexadecimal number to decimal number also that is to extract whatever digit from a hexadecimal number we accept to multiply the number with base 16 so add together it to the decimal value. Allow us consider an example below to see how we tin can extract decimal numbers from a hexadecimal number.

Instance:

Hexadecimal number = 1AB

Equally discussed above we saw A represents 10 and B represents 11. Now nosotros will multiply with proper base of operations with ability of sixteen. So

1 = 1 * (16^ii) = 256

A = x * (xvi^1) = 160

B = 11 * (sixteen^0) = eleven

So now we have to add all these 3 results to obtain decimal value.

256 + 160 + xi = 427

Therefore the decimal value for hexadecimal 1AB is 427.

Instance #2

Beneath is the plan for converting given a hexadecimal number to decimal number:

Lawmaking:

#include<stdio.h>
#include<conio.h>
#include<math.h>
int chief()
{
int decnum=0, rem, i=0, len=0;
char hexdecnum[xx];
printf("Enter any Hexadecimal Number to convert it to decimal number: ");
scanf("%s", hexdecnum);
while(hexdecnum[i]!='\0')
{
len++;
i++;
}
len--;
i=0;
while(len>=0)
{
rem = hexdecnum[len];
if(rem>=48 && rem<=57)
rem = rem-48;
else if(rem>=65 && rem<=90)
rem = rem-55;
else
{
printf("\n Invalid Hexadecimal digit");
getch();
return 0;
}
decnum = decnum + (rem*pw(sixteen, i));
len--;
i++;
}
printf("\nDecimal Value of entered Hexadecimal number = %d", decnum);
getch();
return 0;
}

Output:

Hexadecimal in C Example 2

Enter whatsoever Hexadecimal Number to convert information technology to decimal number: 1AB

Decimal Value of entered Hexadecimal number = 427

In the higher up program, we are converting a hexadecimal number 1AB to a decimal number as 427.

Recommended Articles

This is a guide to Hexadecimal in C. Hither we talk over the Introduction to Hexadecimal in C and its Working too as Examples along with its Code Implementation. You can also go through our other suggested manufactures to learn more –

  1. Prime Numbers in C | Examples
  2. How to Reverse Number in C?
  3. Introduction to Reverse String in C
  4. Prime Numbers in Java | Elevation 3 Examples

morissettedrined.blogspot.com

Source: https://www.educba.com/hexadecimal-in-c/

Post a Comment for "Read Files With Scanner With Hexadecimal Formatting"