![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert an integer to a hex string in C++
In this program we will see how to convert an integer to hex string. To convert an integer into hexadecimal string we can follow mathematical steps. But in this case we have solved this problem using simple trick.
In C / C++ there is a format specifier %X. It prints the value of some variable into hexadecimal form. We have used this format specifier to convert number into a string by using sprintf() function.
Input: An integer number 255 Output: FF
Algorithm
Step 1:Take a number from the user Step 2: Make a string after converting number using %X format specifier Step 3: Print the result. Step 4: End
Example Code
#include<iostream> using namespace std; main() { int n; char hex_string[20]; cout << "Enter a number: "; cin >> n; sprintf(hex_string, "%X", n); //convert number to hex cout << hex_string; }
Output
Enter a number: 250 FA
Advertisements