Write a function itob (int num,char s[], int n) and convert the integer num to the number of n binary. Saved to S.
In the subject, the binary, octal and decimal algorithm, the same way, take the mode, output the number of each position, followed by reverse output. In hex "0123456789abcdef" [num%16], find out the numbers.
#include <stdio.h>void reverse (int len,char arr[]) //reverse { int left =0; int right =len -1; while (left < right) { char temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; right --; left ++; }}void itob (int num,char s[], int n) //Output { int i=0; while (num) in n-binary form { if (n<10) { s[i]=num% n+ ' 0 '; num /= n ; //die Removal i++; } else if (n==16) { s[i]= "0123456789abcdef" [num%16]; num/=16; i++; } else { break; } } s[i]= '   ';; reverse (i , s);} Int main () { char s[32]; int n=0; int num; scanf ("%d", &n); printf ("num="); &NBSP;SCANF ("%d", &num); itob (num,s, n); printf ("%s\n", s); return 0;}
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/76/DD/wKiom1ZdynnjqYYxAAAvaRD8298335.png "title=" capture. PNG "alt=" Wkiom1zdynnjqyyxaaavard8298335.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/76/DD/wKiom1Zdyq6yvP-vAAAu3gTY94o766.png "style=" float: none; "Title=" Captures 1. PNG "alt=" Wkiom1zdyq6yvp-vaaau3gty94o766.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/76/DD/wKiom1Zdyq_DenkxAABDCnzpsfI622.png "style=" float: none; "Title=" Captures 2. PNG "alt=" Wkiom1zdyq_denkxaabdcnzpsfi622.png "/>
This article is from the "Shuoyuexinmao Cloud" blog, please be sure to keep this source http://19940325.blog.51cto.com/10789287/1718698
Write a function itob (int num,char s[], int n) and convert the integer num to the number of n binary. Saved to S.