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
- Program for decimal to hexadecimal conversion
- Program to Convert Hexadecimal Number to Binary
- Check the divisibility of Hexadecimal numbers
- Find the count of natural Hexadecimal numbers of size N
- Convert Binary fraction to Decimal
- Convert a binary number to hexadecimal number
- Convert decimal fraction to binary number
- Convert from any base to decimal and vice versa
- Sum of first N natural numbers by taking powers of 2 as negative number
- Number of decimal numbers of length k, that are strict monotone
- Number of digits before the decimal point in the division of two numbers
- Total ways of selecting a group of X men from N men with or without including a particular man
- Reverse bytes of a Hexadecimal Number
- Number of sub arrays with negative product
- Random list of M non-negative integers whose sum is N
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.
Improved By : AnkitRai01