Write a function itob (int num,char s[], int n) and convert the integer num to the number of n binary. Saved to S.

Source: Internet
Author: User


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]= ' &nbsp ';; 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.