# converting_numbers_from_one_base_to_another.c

Nov 26th, 2019
371
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
2.     Unfinished...
3.     It will be continued...
4.
5.     converting_numbers_from_one_base_to_another.c   by Dragan Milicev
6.
7.
8.     There are 4 bases of numbers that we work with: 2, 8, 10, 16.
9.
10.     2
11.     Binary number system is a base 2 number system.
12.     It uses only two symbols i.e. 0 and 1 to represent all numbers.
13.
14.     8
15.     Octal number system is a base 8 number system.
16.     It uses 8 symbols to represent all its numbers i.e. 01234567
17.
18.     10
19.     Decimal number system is a base 10 number system.
20.     It uses 10 symbols to represent all its numbers i.e. 0123456789
21.
22.     16
23.     Hexadecimal number system is a base 16 number system.
24.     It uses 16 symbols to represent all numbers i.e. 0123456789ABCDEF
25.
26.     There 12 functions for conversions:
27.
28.     2-8     2-10    2-16
29.     8-10    8-16
30.     10-16
31.
32.     16-10   16-8    16-2
33.     10-8    10-2
34.     8-2
35.
36.     In case you want to work with long binary numbers like 20 bits or 30 bit,
37.     you can use string variable to store the binary numbers.
38.
39.     Functions convert string numbers to string numbers or
40.     numbers to numbers, so there are 12 * 2 = 24 functions.
41.
42. //  1. 2-8, (strings)
43. //  2. 2-8, (numbers), DONE
44. //  3. 2-10, (strings) DONE
45. //  4. 2-10, (numbers) DONE
46. //  5. 2-16, (strings)
47. //  6. 2-16, (numbers), impossible because hexadecimal letters cannot be represented by integer
48.
49. //  7. 8-10, (strings)
50. //  8. 8-10, (numbers)
51. //  9. 8-16, (strings)
52. // 10. 8-16, (numbers), impossible because hexadecimal letters cannot be represented by integer
53.
54. // 11. 10-16, (strings) DONE
55. // 12. 10-16, (numbers), impossible because hexadecimal letters cannot be represented by integer
56.
57. // 13. 16-10, (strings) DONE
58. // 14. 16-10, (numbers), impossible because hexadecimal letters cannot be represented by integer
59. // 15. 16-8, (strings)
60. // 16. 16-8, (numbers), impossible because hexadecimal letters cannot be represented by integer
61. // 17. 16-2, (strings)
62. // 18. 16-2, (numbers), impossible because hexadecimal letters cannot be represented by integer
63.
64. // 19. 10-8, (strings)
65. // 20. 10-8, (numbers)
66. // 21. 10-2, (strings), DONE
67. // 22. 10-2, (numbers), DONE
68.
69. // 23. 8-2, (strings), DONE
70. // 24. 8-2, (numbers), DONE
71.
72.
73.     You can find all my C programs at Dragan Milicev's pastebin:
74.
75.     https://pastebin.com/u/dmilicev
76.
77. */
78.
79. #include <stdio.h>
80. #include <string.h>
81. #include <math.h>           // for function pow()
82.
83. #define MAX_SIZE 100
84.
85.
86.
87. // 1. 2-8, (strings), binary to octal
88. // Function to convert string of a Binary Number to an string of Octal Number
89. // https://www.knowprogram.com/c-programming/convert-octal-to-binary-and-binary-to-octal/
90. // With decimal point:
91. // https://www.geeksforgeeks.org/convert-binary-number-octal/
92. char *BinToOct_Strings( char *binNumS, char *octNumS )
93. {
94. // ... add code ...
95.     return octNumS;
96. } // BinToOct_Strings()
97.
98.
99. // 2. 2-8, (numbers), binary to octal
100. // Function to convert an Binary Number to Octal Number
101. // https://www.programiz.com/c-programming/examples/octal-binary-convert
102. // https://codeforwin.org/2015/08/c-program-to-convert-binary-to-octal-number-system.html
103. // https://www.sanfoundry.com/c-program-convert-binary-octal/
104. int BinToOct( long long binaryNumber, long long* octalNumber )
105. {
106.     int decimalNumber = 0, i = 0;
107.     //long long octalNumber = 0;
108.
109.     *octalNumber = 0;                       // reset octalNumber
110.
111.     while(binaryNumber != 0)
112.     {
113.         decimalNumber += (binaryNumber%10) * pow(2,i);
114.         i++;
115.         binaryNumber/=10;
116.     }
117.
118.     i = 1;
119.
120.     while (decimalNumber != 0)
121.     {
122.         *octalNumber += (decimalNumber % 8) * i;
123.         decimalNumber /= 8;
124.         i *= 10;
125.     }
126.
127.     return *octalNumber;
128. } // BinToOct()
129.
130.
131. // 3. 2-10, (strings), binary to decade
132. // Function to convert string of a Binary Number to a string of Decade Number
133. char *BinToDec_Strings( char *binNumS, char *convertedNumS )
134. {
135.     int i;
136.     int dec_value = 0;
137.     int base = 1;                   // Initializing base value to 1, i.e 2^0
138.     int len = strlen(binNumS);
139.
140.     strcpy(convertedNumS,"");       // empty string convertedNumS
141.
142.     for ( i=len-1; i>=0; i--) {
143.
144.         if ( *(binNumS+i) == '1' )
145.             dec_value += base;
146.
147.         base = base * 2;
148.     }
149.
150.     sprintf(convertedNumS,"%d",dec_value);  // convert dec_value to string
151.
152.     return convertedNumS;
153. } // BinToDec_Strings
154.
155.
156. // 4. 2-10, (numbers), binary to decade
157. // Function to convert Binary Number to Decade Number
158. long long BinToDec( long long binNum, long long* decNum )
159. {
160.     int base = 1;       // Initializing base value to 1, i.e 2^0
161.     int last_digit;
162.
163.     *decNum = 0;    // reset decNum
164.
165.     while( binNum ) {
166.         last_digit = binNum % 10;
167.
168.         binNum = binNum / 10;
169.
170.         *decNum += last_digit * base;
171.
172.         base = base * 2;
173.     }
174.
175.     return *decNum;
176. } // BinToDec
177.
178.
179. // 5. 2-16, (strings)
180. // 6. 2-16, (numbers), impossible because hexadecimal letters cannot be represented by integer
181.
182. // 7. 8-10, (strings)
183. // 8. 8-10, (numbers)
184. // 9. 8-16, (strings)
185. // 10. 8-16, (numbers), impossible because hexadecimal letters cannot be represented by integer
186.
187.
189. // Function to convert string of a Decade Number to a string of a Hexadecimal Number
191. char *DecToHex_Strings( char *decNumS, char *convertedNumS )
192. {
193.     int temp;                   // temp is temporary variable to store remainder
194.     int i=0;                    // i is counter for string decNumS
195.     int j;                      // j is counter for reverse order
196.     int k=0;                    // k is counter for convertedNumS
197.     int dec_num = atoi(decNumS);// convert string decNumS to integer dec_num
198.     int len;                    // lenght of convertedNumS
199.     char mem_char;
200.
201.     strcpy(convertedNumS,"");   // empty string convertedNumS
202.
203.     while( dec_num != 0 )
204.     {
205.         temp  = 0;              // temporary variable to store remainder
206.
207.         temp = dec_num % 16;    // storing remainder in temp variable
208.
209.         if( temp < 10 )         // check if temp < 10
210.             *(convertedNumS + (i++) ) = temp + 48;
211.         else
212.             *(convertedNumS + (i++) ) = temp + 55;
213.
214.         dec_num = dec_num / 16;
215.     }
216.
217.     *(convertedNumS+i) = '\0';  // terminate the string convertedNumS
218.
219.     len = strlen(convertedNumS);
220.
221.     for( i=0,j=len-1; i<len/2; i++,j-- )    // reverse string convertedNumS
222.     {
223.         mem_char = *(convertedNumS+i);
224.         *(convertedNumS+i) = *(convertedNumS+j);
225.         *(convertedNumS+j) = mem_char;
226.     }
227.
228.     return convertedNumS;
229. } // DecToHex_Strings
230.
231.
232. // 12. 10-16, (numbers), impossible because hexadecimal letters cannot be represented by integer
233.
234.
236. // Function to convert string of a Hexadecimal Number to a string of Decade Number
238. char *HexToDec_Strings( char *hexNumS, char *convertedNumS )
239. {
240.     int base = 1;               // Initializing base value to 1, i.e 16^0
241.     int i, dec_val = 0;
242.     int len=strlen(hexNumS);
243.
244.     strcpy(convertedNumS,"");   // empty string convertedNumS
245.
246.     for( i=len-1; i>=0; i-- )   // Extracting characters as digits from last character
247.     {
248.         // if character lies in '0'-'9', converting it to integral 0-9
249.         // by subtracting 48 from ASCII value.
250.         if( *(hexNumS+i)>='0' && *(hexNumS+i)<='9' )
251.         {
252.             dec_val += (*(hexNumS+i) - 48) * base;
253.             base = base * 16;   // incrementing base by power
254.         }
255.
256.         // if character lies in 'A'-'F' , converting it to integral 10-15
257.         // by subtracting 55 from ASCII value
258.         else if( *(hexNumS+i)>='A' && *(hexNumS+i)<='F' )
259.         {
260.             dec_val += ( *(hexNumS+i) - 55) * base;
261.             base = base * 16;   // incrementing base by power
262.         }
263.     }
264.
265.     sprintf(convertedNumS, "%d", dec_val ); // convert dec_val to string
266.
267.     return convertedNumS;
268. } // BinToDec_Strings
269.
270.
271. // 14. 16-10, (numbers), impossible because hexadecimal letters cannot be represented by integer
272. // 15. 16-8, (strings)
273. // 16. 16-8, (numbers), impossible because hexadecimal letters cannot be represented by integer
274. // 17. 16-2, (strings)
275. // 18. 16-2, (numbers), impossible because hexadecimal letters cannot be represented by integer
276.
277. // 19. 10-8, (strings)
278. // 20. 10-8, (numbers)
279.
280.
281. // 21. 10-2, (strings), decade to binary
282. // Function to convert string of a Decade Number to a string of Binary Number
283. // https://www.includehelp.com/c-programs/c-program-to-convert-number-from-decimal-to-binary.aspx
284. // https://codereview.stackexchange.com/questions/192390/convert-binary-string-to-decimal-number
285. char *DecToBin_Strings( char *decNumS, char *convertedNumS )
286. {
287.     int number, i, j=0, r ;     // i for bin[], j for convertedNumS, r for reverse order
288.     int bin[MAX_SIZE];
289.
290.     strcpy(convertedNumS,"");   // empty string convertedNumS
291.
292.     number = atoi(decNumS);     // convert string to integer
293.
294.     i=0;                        // initialize index to zero
295.
296.     while( number>0 )
297.     {
298.         bin[i] = number % 2;
299.         number = number / 2;
300.         i++;
301.     }
302.
303.     // print values of bin[r] in reverse order to convertedNumS
304.     for( r=i-1; r>=0; r-- )
305.         sprintf( convertedNumS + (j++), "%d", bin[r] );
306.
307.     *(convertedNumS+j) = '\0';  // terminate the string convertedNumS
308.
309.     return convertedNumS;
310. } // BinToDec_Strings
311.
312.
313. // 22. 10-2, (numbers), decade to binary
314. // Function to convert Decade Number to Binary Number
315. // https://www.programiz.com/c-programming/examples/binary-decimal-convert
316. // https://www.w3resource.com/c-programming-exercises/for-loop/c-for-loop-exercises-41.php
317. long long DecToBin( long long decNum, long long* binNum )
318. {
319.     int remainder, i=1, step=1;
320.
321.     *binNum = 0;        // reset binNum
322.
323.     while (decNum!=0)
324.     {
325.         remainder = decNum % 2;
326.         decNum /= 2;
327.         *binNum += remainder * i;
328.         i *= 10;
329.     }
330.     return *binNum;
331. } // BinToDec
332.
333.
334. // 23. 8-2, (strings) octal to binary
335. // Function to convert string of an Octal Number to Binary Number string
336. // https://www.geeksforgeeks.org/program-to-convert-octal-number-to-*binary-number/
337. char *OctToBin_Strings( char *octNumS, char *convertedNumS )
338. {
339.     int i = 0;
340.
341.     strcpy(convertedNumS,"");       // empty string convertedNumS
342.
343.     while (octNumS[i]) {
344.         switch (octNumS[i]) {
345.         case '0':
346.             strcat(convertedNumS,"000");
347.             break;
348.         case '1':
349.             strcat(convertedNumS,"001");
350.             break;
351.         case '2':
352.             strcat(convertedNumS,"010");
353.             break;
354.         case '3':
355.             strcat(convertedNumS,"011");
356.             break;
357.         case '4':
358.             strcat(convertedNumS,"100");
359.             break;
360.         case '5':
361.             strcat(convertedNumS,"101");
362.             break;switch (octNumS[i])
363.         case '6':
364.             strcat(convertedNumS,"110");
365.             break;
366.         case '7':
367.             strcat(convertedNumS,"111");
368.             break;
369.         default:
370.             printf("\n Invalid Octal Digit %c \n", octNumS[i] );
371.             strcpy(convertedNumS,"");
372.             return(convertedNumS);          // on error return empty string
373.             break;
374.         } // switch (octNumS[i])
375.
376.         i++;
377.
378.     } // while (octNumS[i])
379.
380.     return convertedNumS;
381. } // OctToBin_Strings()
382.
383.
384. // 24. 8-2, (numbers), octal to binary
385. // Function to convert an Octal Number to Binary Number
386. // https://www.programiz.com/c-programming/examples/octal-binary-convert
387. // https://www.geeksforgeeks.org/program-to-convert-octal-number-to-binary-number/
388. // another way:
389. // https://codeforwin.org/2015/08/c-program-to-convert-octal-to-binary-number-system.html
390. long long OctToBin(long long octalNumber, long long* binaryNumber )
391. {
392.     int decimalNumber = 0, i = 0;
393.     //long long binaryNumber = 0;
394.
395.     *binaryNumber = 0;                      // reset binaryNumber
396.
397.     while(octalNumber != 0)
398.     {
399.         decimalNumber += (octalNumber%10) * pow(8,i);
400.         ++i;
401.         octalNumber/=10;
402.     }
403.     i = 1;
404.     while (decimalNumber != 0)
405.     {
406.         *binaryNumber += (decimalNumber % 2) * i;
407.         decimalNumber /= 2;
408.         i *= 10;
409.     }
410.     return *binaryNumber;
411. } // OctToBin()
412.
413.
414.
415. int main(void)
416. {
417.     // numbers in the form of a string, string numbers
418.     char binNumS[MAX_SIZE] = "1010";// binary      number can have  2 digits 01
419.     char octNumS[MAX_SIZE] = "70";  // octal       number can have  8 digits 01234567
420.     char decNumS[MAX_SIZE] = "26";  // decimal     number can have 10 digits 0123456789
421.     char hexNumS[MAX_SIZE] = "1A";  // hexadecimal number can have 16 digits 0123456789ABCDEF
422.     char convertedNumS[MAX_SIZE] = "";
423.
424.     // numbers
425.     long long binNum = 1011;
426.     long long octNum = 71;
427.     long long decNum = 12;
428.     long long LongLongNum = 11;
429.
430.
431. /*
432.     // 1. 2-8, binary to octal (strings)
433.     printf("\n\t 1. 2-8, binary to octal (strings) \n");
434.     BinToOct_Strings( binNumS, convertedNumS );
435.     printf("\n binary %s = %s octal (strings) \n", binNumS, convertedNumS );
436.     printf("\n binary %s = %s octal (strings) \n\n", binNumS, BinToOct_Strings(binNumS,convertedNumS) );
437. */
438.
439.     // 2. 2-8, binary to octal (numbers)
440.     printf("\n\t 2. 2-8, binary to octal (numbers) \n");
441.     octNum = BinToOct( binNum, &octNum );
442.     printf("\n binary %lld = %lld octal \n", binNum, octNum );
443.     printf("\n binary %lld = %lld octal \n\n", binNum, BinToOct(binNum,&octNum) );
444.
445.     // 3. 2-10, binary to decade (strings)
446.     printf("\n\t 3. 2-10, binary to decade (strings) \n");
447.     BinToDec_Strings( binNumS, convertedNumS );
448.     printf("\n binary %s = %s decade \n", binNumS, convertedNumS );
449.     printf("\n binary %s = %s decade \n\n", binNumS, BinToDec_Strings(binNumS,convertedNumS) );
450.
451.     // 4. 2-10, binary to decade (numbers)
452.     printf("\n\t 4. 2-10, binary to decade (numbers) \n");
453.     LongLongNum = BinToDec( binNum, &LongLongNum );
454.     printf("\n binary %lld = %lld decade \n", binNum, LongLongNum );
455.     printf("\n binary %lld = %lld decade \n\n", binNum, BinToDec(binNum,&LongLongNum) );
456.
457.
460.     DecToHex_Strings( decNumS, convertedNumS );
463.
466.     HexToDec_Strings( hexNumS, convertedNumS );
469.
470.
471.     // 21. 10-2, decade to binary (strings)
472.     printf("\n\t 21. 10-2, decade to binary (strings) \n");
473.     DecToBin_Strings( decNumS, convertedNumS );
474.     printf("\n decade %s = %s binary \n", decNumS, convertedNumS );
475.     printf("\n decade %s = %s binary \n\n", decNumS, DecToBin_Strings(decNumS,convertedNumS) );
476.
477.     // 22. 10-2, decade to binary (numbers)
478.     printf("\n\t 22. 10-2, decade to binary (numbers) \n");
479.     LongLongNum = DecToBin( decNum, &LongLongNum );
480.     printf("\n decade %lld = %lld binary \n", decNum, LongLongNum );
481.     printf("\n decade %lld = %lld binary \n\n", decNum, DecToBin(decNum,&LongLongNum) );
482.
483.     // 23. 8-2, octal to binary (strings)
484.     printf("\n\t 23. 8-2, octal to binary (strings) \n");
485.     OctToBin_Strings( octNumS, convertedNumS );
486.     printf("\n octal %s = %s binary (strings) \n", octNumS, convertedNumS );
487.     printf("\n octal %s = %s binary (strings) \n\n", octNumS, OctToBin_Strings(octNumS,convertedNumS) );
488.
489.     // 24. 8-2, octal to binary (numbers)
490.     printf("\n\t 24. 8-2, octal to binary (numbers) \n");
491.     LongLongNum = OctToBin(octNum, &LongLongNum);
492.     printf("\n octal %lld = %lld binary \n", octNum, LongLongNum );
493.     printf("\n octal %lld = %lld binary \n\n", octNum, OctToBin(octNum,&LongLongNum) );
494.
495.
496.     return 0;
497. }