Given a number N in decimal format, the task is to convert it to the hexadecimal representation of N as a string. Negative numbers are stored in 2’s complement form.
Input: N = 134
134 = 00000000000000000000000010001000 in 32 bit representation. Grouping in four-size chunks and converting each chunk to equivalent hexadecimal yields 88. Also, we can see 8*16 + 8 = 134. We will also get the same result by remainder technique discussed in other post.
Input: N = -1
The ides is to store negative numbers in a bigger size to trick the compiler to read it as positive instead of negative and then use the normal remainder technique. Store num in a u_int, size of u_it is greater, it will be positive since MSB is 0.
Below is the implementation of the above approach:
Hexa representation for 134 is 86 -1 is ffffffff -234 is ffffff16
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program for decimal to hexadecimal conversion
- Total ways of selecting a group of X men from N men with or without including a particular man
- Count of contiguous subarrays possible for every index by including the element at that index
- Program to Convert Octal to Hexadecimal
- Program to Convert Hexadecimal Number to Binary
- Convert a binary number to hexadecimal number
- Find the count of natural Hexadecimal numbers of size N
- Check the divisibility of Hexadecimal numbers
- Largest Even and Odd N-digit numbers in Hexadecimal Number System
- Largest and Smallest N-digit Hexadecimal Numbers
- How to add two Hexadecimal numbers
- Convert from any base to decimal and vice versa
- Convert decimal fraction to binary number
- Convert a given Decimal number to its BCD representation
- Program to convert Hexa-Decimal Number to its equivalent BCD
- Program to Convert BCD number into Decimal number
- Program to convert a BCD to Hexa-Decimal Number
- Convert given Decimal number into an irreducible Fraction
- Convert Binary fraction to Decimal
- Reverse bytes of a Hexadecimal Number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.