Prototype:
1 // turn binary 2 char * ConvertTo2String ( Long Number); . 3 // convert hexadecimal . 4 char * ConvertTo16String ( Long Number);
Ideas:
Binary conversion is very simple, two-step operation to complete,
1: number for the recycling and the operation of the digital bits and a left 31-i (i = {0,31}) bits (high-order), and,
2: the results then just right 31-i (i = {0,31}) obtained per one bit is 0 or 1,
This was every bit, and then these bits makes up the string on the OK!
1 char *ConvertTo2String(long number) 2 { 3 char *output = NULL; 4 output = (char*)malloc(33); //include '\0' 5 6 int i = 0; 7 for(;i<32;i++) 8 { 9 output[i] = number & (1<<31-i); 10 output[i] = output[i] >> 31-i; 11 output[i] = (output[i] == 0) ? '0' : '1'; 12 } 13 output[i] = '\0'; 14 return output 15 }
Converts a hexadecimal trouble that we have to consider the case of letters
1 char * ConvertTo16String(long number) 2 { 3 char *output= NULL; 4 char *temp = NULL; 5 6 output= (char*) malloc(11); 7 8 output[0] = '0'; 9 output[1] = 'x'; 10 output[10] = '\0'; 11 + = Output TEMP 2 ; 12 is 13 is for ( int I = 0 ; I < . 8 ; I ++ ) 14 { 15 TEMP [I] = ( char ) (<< Number 4 * I >> 28 ); // first left 4 * i, then the right 28, each treatment 4 16 TEMP [I] = TEMP [I]> = 0 TEMP [I]:? TEMP [I] + 16 ; // to convert letters a ~ F prepare . 17 TEMP [I] = TEMP [I] < 10 ? TEMP [I] + 48 : TEMP [I] + 55 ; // turn letters 18 } 19 20 return output; 21 }