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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Program for decimal to hexadecimal conversion
- Program to Convert Octal to Hexadecimal
- Program to Convert Hexadecimal Number to Binary
- Check the divisibility of Hexadecimal numbers
- Largest and Smallest N-digit Hexadecimal Numbers
- Find the count of natural Hexadecimal numbers of size N
- Largest Even and Odd N-digit numbers in Hexadecimal Number System
- Convert a given Decimal number to its BCD representation
- Convert Binary fraction to Decimal
- Convert decimal fraction to binary number
- Program to convert a BCD to Hexa-Decimal Number
- Convert from any base to decimal and vice versa
- Convert a binary number to hexadecimal number
- Program to convert Hexa-Decimal Number to its equivalent BCD
- Sum of first N natural numbers by taking powers of 2 as negative 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 email@example.com. 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.