EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Decimal to Hexadecimal in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Decimal to Hexadecimal in C

Decimal to Hexadecimal in C

Introduction to Decimal to Hexadecimal in C

Sometimes there is a mathematical calculation in programming for making that calculation happens we need to convert from decimal to hexadecimal which can be done in C easily in various ways. A decimal number includes numbers from 0 to 9 and the base number is 10 whereas for hexadecimal it includes numbers from 0 to 9 while including A, B, C, D, E, F and the base number is 16. Therefore, whenever a user is giving a decimal number as input we need to convert it into a hexadecimal number from base 10 to base 16.

Syntax

Let’s have a look at the syntax and alongside we will see the steps for converting a number from decimal to hexadecimal :

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Step 1: We have to divide the given number by 16 because we are converting the number into hexadecimal number.

Step 2: After that we have to again divide the remaining quotient by 16.

Step 3: We have to keep dividing the remaining quotient until our quotient turns to zero.

To explain the above step practically let’s take a number and convert it into the hexadecimal number.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Convert the number 800 in the hexadecimal

Step 1:Divide the number by 16. Therefore, 800 / 16 , Remainder : 0 , Quotient : 50

Step 2: Divide the quotient by 16. Therefore, 50 / 16, Remainder : 2 , Quotient : 3

Step 3: Divide the quotient by 16. Therefore, 3 / 16, Remainder : 3 , Quotient : 0

Final result, the converted decimal to hexadecimal number is: 320

( 800 ) 10 = ( 320 ) 16

As we have understood the process of converting the decimal number into hexadecimal mathematically, Now we will see programmatically to implement the algorithm to convert the number.

Examples to Implement Decimal to Hexadecimal in C

Below are the examples mentioned:

Example #1

Code:

#include<stdio.h>
int main() {
long int num_decimal , remainder , quotient ;
int a = 1 , b , var ;
char hexanum_decimal[ 100 ] ;
printf( " Please enter decimal number here : " ) ;
scanf( "%ld" , &num_decimal ) ;
quotient = num_decimal ;
while( quotient != 0 ) {
var = quotient % 16 ;
if( var < 10 )
var = var + 48 ;
else
var = var + 55 ;
hexanum_decimal[ a++ ]= var ;
quotient = quotient / 16;
}
printf( " The equivalent hexadecimal value of decimal number is %ld : " , num_decimal ) ;
for ( b = a -1 ; b > 0 ; b-- )
printf( "%c" , hexanum_decimal[ b ] ) ;
return 0 ;
}

Output :

decimal to hexadecimal in c1

Explanation: As you can see in the above code, we are defining three long integer type variable named as “ num_decimal ”, “ remainder ”, “ quotient ”. Then we have declared integer variables a, b, and var where the value of integer variable a is assigned value equals 1. Then for hexadecimal number, we have to declare it as a character because it includes character values also. After that printf and scanf are used to take value from the user and display it on the screen.

Then the quotient value will be set equal to decimal number as of step 1 we have studied above. We are adding while the condition in which it will perform step 2 and step 3 until the quotient value becomes zero. If the condition is used to convert the given integer into the character. Finally, once quotient turns 0 it will print the hexadecimal value of the given decimal number.

Example #2

Code:

#include <stdio.h>
int main()
{
int num_decimal , temp , a ;
char hex [ 32 ] ;
printf( " Please enter the decimal number ( num_decimal ) here : " ) ;
scanf( "%d", &num_decimal ) ;
temp = 0 ;
while( num_decimal > 0 )
{
switch( num_decimal % 16 )
{
case 10 :
hex [ temp ] = 'A' ; break ;
case 11 :
hex [ temp ] = 'B'; break ;
case 12 :
hex [ temp ] = 'C' ; break ;
case 13 :
hex [ temp ] = 'D'; break ;
case 14 :
hex [ temp ] = 'E' ; break ;
case 15 :
hex [ temp ] = 'F'; break ;
default :
hex [ temp ] = ( num_decimal%16 ) + 0x30 ;
}
num_decimal = num_decimal / 16 ;
temp ++ ;
}
printf( " The Hexadecimal value of the decimal number is: " ) ;
for( a= ( temp-1 ) ; a >= 0 ; a--)
printf( "%c" , hex[ a ] ) ;
return 0;
}

Output :

decimal to hexadecimal in c2

Explanation: As you can see the above code will perform the same set of operations but in switch and break statement way. We are taking the same value as input which you can see in the output screen. we are defining three integer type variable named as “ num_decimal ”, “ temp ”, “ a ”. Then for hexadecimal number, we have to declare it as a character because it includes character values also from  A to F. After that printf and scanf are used to take value from the user and display it on the screen. We are adding while the condition in which it will perform a switch statements according to the condition applied on switch whenever we get temp value. If the condition is used to convert the given integer into the character. After that we will convert the value into hexadecimal value.

Conclusion

Conversion of decimal number into hexadecimal using the above steps saves a huge amount of time in programming because of quick and correct results in the smallest possible time. In the case of large decimal numbers, this logic is proved to be efficient in many norms in computer programming.

Recommended Articles

This is a guide to Decimal to Hexadecimal in C. Here we discuss an introduction to Decimal to Hexadecimal in C, with appropriate syntax and respective examples. You can also go through our other related articles to learn more –

  1. Hexadecimal in C 
  2. Arrays in C Programming
  3. Functional Programming in Java
  4. Programming Errors in C

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training Course Learn More