How to Print a Hexadecimal Value in C (2024)

  1. An Overview of Hexadecimal Numbers in C Programming Language
  2. Format Specifiers in C Programming Language
  3. The Process of Conversion to a Hexadecimal Value
  4. Implementation of Printing Hexadecimal Values
  5. Use %x to Print Hexadecimal Value in Small Letters in C
  6. Use %X to Print Hexadecimal Value in Small Letters in C
How to Print a Hexadecimal Value in C (1)

This post will discuss printing hexadecimal values using the C programming language.

An Overview of Hexadecimal Numbers in C Programming Language

A hexadecimal number is a value constructed using 16 symbols in the C programming language. These symbols include the ten normal numerical systems ranging from 0 to 9 and the six additional symbols ranging from A to F.

The hexadecimal number system in C is called the base-16 number system. It is impossible to save hexadecimal numbers in a data type such as float, long, or double; however, you can store them in an integral data type.

To input a hexadecimal value in the C programming language, you must use scanf, which provides format specifiers such as %x and %X.

A hexadecimal value is represented in the C programming language as either 0x or 0X. To print an integer number in hexadecimal format, the format specifier %x or %X must be used inside the printf() command.

The value is printed in hexadecimal format with the alphabets in lowercase when %x is used, as (a-f). The value is printed in hexadecimal format with the alphabets in uppercase when %X is used, as (A-F).

Format Specifiers in C Programming Language

If you want 0x to always be included in the output, you shouldn’t use the # symbol since it causes 0x to be prepended to the output even if the value is 0.

Producing leading zeroes may be accomplished by combining the width field with the 0 flags in the following manner: the value of %08x adds leading zeroes to the integer so that it has a width of 8.

There is also the possibility of using the precision field, which reads as follows: %.8x pads the integer with leading zeroes to make it a total of 8 digits. Therefore, 0x%.8x is another option for you to consider for your goal.

If a prefix is formed as part of the conversion, such as 0x for # or - for negative integers in signed conversions, these conversion requirements will be different. The length of the prefix will be counted for the width, but it will not be counted for the accuracy specifier.

Here’s an example of how various specifiers may be used to provide various output styles for hexadecimal numbers.

#include <stdio.h>int main() { printf("%10x\n", 252); printf("%010x\n", 252); printf("%#010x\n", 252); printf("%10.8x\n", 252); printf("%#10.8x\n", 252); printf("0x%.8x\n", 252); printf("%10x\n", 0); printf("%010x\n", 0); printf("%#010x\n", 0); printf("%10.8x\n", 0); printf("%#10.8x\n", 0); printf("0x%.8x", 252);}

Output:

 fc00000000fc0x000000fc 000000fc0x000000fc0x000000fc 000000000000000000000 00000000 000000000x000000fc

The Process of Conversion to a Hexadecimal Value

Now, let’s look at how the system does this conversion so we can fully comprehend it.

  1. You first need to divide the supplied number by 16 and note the remainder of the operation.
  2. Continue to do step 1 until the quotient equals 0.
  3. Combine the remaining components in the opposite sequence in which they were acquired.

Implementation of Printing Hexadecimal Values

In this example, for instance, when we divide 252 by 16, we get 15 as the quotient and 12 as the remainder. Now, we will divide 15 by 16; the quotient will be 0, while the remainder will be 15.

It is important to note that when 15 is divided by 16, the resulting quotient is 0. So, here is where we stop calculating.

The two remainders acquired after the first two stages are 12 and 15, and their corresponding representations in hexadecimal are c and f. The hexadecimal representation of remainders is organized here in the opposite direction of how they were produced.

Therefore, the value 252 will be represented in hexadecimal as fc in this instance.

Use %x to Print Hexadecimal Value in Small Letters in C

The hexadecimal value will be printed using the %x format modifier in tiny letters. Like, ff, ffd, etc.

Let us look at the following example to see how the %x format specifier works.

The first step is to import the necessary libraries and construct a main() function.

Next, create a variable with the datatype int and name it integerValue. Then, give it an integer value that will be converted into the corresponding hexadecimal value.

Then, we need to convert the integer value to hexadecimal, which the %x specifier will automatically accomplish for us and print the value in small letters.

Code:

#include <stdio.h>int main() { int integerValue = 252; printf("The converted value in hexadecimal is: %x\n", integerValue); return 0;}

Output:

The converted value in hexadecimal is: fc

Use %X to Print Hexadecimal Value in Small Letters in C

In this example, we will use %X in the print statement to output the value in capital letters.

#include <stdio.h>int main() { int integerValue = 252; printf("The converted value in hexadecimal is: %X\n", integerValue); return 0;}

Output:

The converted value in hexadecimal is: FC
How to Print a Hexadecimal Value in C (2024)

FAQs

How to print out hexadecimal in C? ›

So, in order to print a hex number, you just need to take the number you want to print, 4 bits at a time, and convert it from the integer 0-15 to the characters '0' to '9' and then 'A' to 'F'. To get the number 4 bits at a time, you just shift and mask it, eg. uint8_t test = 0x3F //This is the number we want to print.

How to pass hexadecimal value in C? ›

In C programming language, hexadecimal value is represented as 0x or 0X and to input hexadecimal value using scanf which has format specifiers like %x or %X.

How to add hexadecimal numbers in C? ›

Start with carrying zero and add both numbers(with the carry) from the end and update carry in each addition. Perform the same operation on the remaining length of the other string (if both the strings have different lengths). Return the value that has been added.

How to generate hex file from C code? ›

You need a compiler (and an assembler and a linker), also known as a toolchain. The compiler converts your C program source code into executable object code for your target machine. This object code can be represented as a series of hex digits in a . hex file.

What is %d in C? ›

In C, format specifiers are represented by special characters that begin with the modulus/ percent symbol (%), followed by a character indicating the data type. For example, %d represents a decimal integer, %f represents a floating-point number, and %c represents a character.

How to declare hexadecimal array in C? ›

unsigned char a[16] = {0x20, 0x41, 0x42, }; will initialise the first three elements as shown, and the remaining elements to 0 . unsigned char a[16] = {"0x20"}; won't do what you want: it just defines a nul-terminated string with the four characters 0x20 , the compiler won't treat it as a hexadecimal value.

How to print decimal values in C? ›

For example, 5.48958123 should be printed as 5.4895 if given precision is 4. In C, there is a format specifier in C. To print 4 digits after dot, we can use 0.4f in printf().

What is the hex value? ›

The hexadecimal number system is a type of number system, that has a base value equal to 16. It is also pronounced sometimes as 'hex'. Hexadecimal numbers are represented by only 16 symbols. These symbols or values are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. Each digit represents a decimal value.

How to print a hex number in C? ›

%x. %x will print the hexadecimal in small letters. Like, aaff2, ffd etc.

How to check if a number is hexadecimal in C? ›

isxdigit() function in C Language

char ch = 'A' ; // checking is the given input is hexadecimal or not?

How to print out ascii value in C? ›

We can print the ASCII values in C of any character by using the %d format specifier. To print all ASCII characters, we can use a loop that iterates through numbers 0 to 255 and then print the characters using %c and %d format specifiers.

References

Top Articles
How to Get an Upgrade on Your Next Flight
Find plane tickets on Google Flights - Computer
Jimmy Johns Delivery Hours
This Modern World Daily Kos
Melissababyxo Cam
Craigslist In Lakeland
Louisville Kentucky Craigslist Cars And Trucks By Owner
Dr Frita Mcrae Fisher Husband
The 8 Best Santa Ynez Wineries to Visit in 2023
Member Handbook 2021 | Ohio Medicaid Caresource | Member Handbook
Astral Ore Calamity
Pokemon Infinite Fusion Good Rod
El Puerto Harrisonville Mo Menu
Jennifer Lenzini Leaving Ktiv
Craigslist Cars For Sale By Owner Oklahoma City
Clemson Sorority Rankings 2022
Trestle Table | John Lewis & Partners
4 Star Brewery
Coffey Funeral Home Tazewell Tn Obituaries
How 'The Jordan Rules' inspired template for Raiders' 'Mahomes Rules'
Omni Id Portal Waconia
Comenity Pay Cp
Bank Of America.aomc
Unveiling The Fascination: Makayla Campinos Video
Food Delivery Near Me Open Now Chinese
Tani Ahrefs
Westgate Trailer Mountain Grove
2010 Ford F-350 Super Duty XLT for sale - Wadena, MN - craigslist
Nephi Veterinarian
Best Fantime Accounts
Stellaris Remove Planet Modifier
Craigslist Pikeville Tn
Hca Florida Middleburg Emergency Reviews
Ixl.prentiss
Nc Scratch Off Left
Apple Watch 9 vs. 10 im Vergleich: Unterschiede & Neuerungen
JetBlue, Spirit end $3.8 billion merger agreement after losing antitrust suit
Gym Membership & Workout Classes in Lafayette IN | VASA Fitness
10 Best Laptops for FL Studio in 2023 | Technize
Kirby D. Anthoney Now
Live Gold Spot Price Chart | BullionVault
Porter House Ink Photos
Fisher-Cheney Funeral Home Obituaries
Rolla Mo Craigslist
2005 Lund Boat For Sale in Ham Lake, MN Lot #67597***
My Compeat Workforce
Top 100 Golfclubs - Albrecht Golf Guide bei 1Golf.eu
‘Covfefe’ tells you all you need to know about Trump | CNN Politics
Clarksburg Wv Craigslist Personals
eCare: Nutzung am PC | BARMER
Dl 9672
The Emperor's New Groove | Rotten Tomatoes
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 6125

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.