Convert Decimal To Hexa | C ++ program to convert decimal to hexadecimal covering negative numbers ; Function to convert decimal no . to hexadecimal number ; map for decimal to hexa , 0 - 9 are straightforward , alphabets a - f used for 10 to 15. ; string to be returned ; check if num is 0 and directly return "0" ; if num > 0 , use normal technique as discussed in other post ; if num < 0 , we need to use the elaborated trick above , lets see this ; store num in a u_int , size of u_it is greater , it will be positive since msb is 0 ; use the same remainder technique . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string Hex ( int num ) { map < int , char > m ; char digit = '0' ; char c = ' a ' ; for ( int i = 0 ; i <= 15 ; i ++ ) { if ( i < 10 ) { m [ i ] = digit ++ ; } else { m [ i ] = c ++ ; } } string res = " " ; if ( ! num ) { return "0" ; } if ( num > 0 ) { while ( num ) { res = m [ num % 16 ] + res ; num /= 16 ; } } else { u_int n = num ; while ( n ) { res = m [ n % 16 ] + res ; n /= 16 ; } } return res ; } int main ( ) { int x = 134 , y = -1 , z = -234 ; cout << " Hexa ▁ representation ▁ for " << endl ; cout << x << " ▁ is ▁ " << Hex ( x ) << endl ; cout << y << " ▁ is ▁ " << Hex ( y ) << endl ; cout << z << " ▁ is ▁ " << Hex ( z ) << endl ; return 0 ; }