C编程 实现输入一个正整数,输出其对应的二进制数。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C编程 实现输入一个正整数,输出其对应的二进制数。相关的知识,希望对你有一定的参考价值。
如果不使用数组,该怎么输出结果呢?
用递归#include "stdio.h"
int output(int num);
void main()
int num;
printf("输入正整数:"); //当超过一定位数时,会溢出
scanf("%d",&num);
if(num<=0)
printf("输入整数!");
else
output(num);
int output(int num)
int temp;
if(num!=0)
temp=num%2;
num=num/2;
output(num);
printf("%d",temp);
参考技术A 1。//实用点
#include <stdio.h>
typedef unsigned char byte;
void ShowBit(byte x, int n)
if (--n) ShowBit(x>>1, n);
printf("%d", x%2);
int main()
byte x = 0x6A;
ShowBit(x, 8);
return 0;
2。//容易点
#include <stdio.h>
int main(void)
struct bits
unsigned char b8 :1,
b7 :1,
b6 :1,
b5 :1,
b4 :1,
b3 :1,
b2 :1,
b1 :1;
;
union uchar
struct bits chbits;
unsigned char ch;
mychar;
printf("Enter a character: ");
scanf("%c", &mychar.ch);
printf("Binary representation of the character %c : %d%d%d%d%d%d%d%d\n", mychar.ch,
mychar.chbits.b1, mychar.chbits.b2, mychar.chbits.b3,
mychar.chbits.b4, mychar.chbits.b5, mychar.chbits.b6,
mychar.chbits.b7, mychar.chbits.b8);
return 0;
3.微软标准答案
////////////////////////////////
static void __cdecl xtoa (
unsigned long val,
char *buf,
unsigned radix,
int is_neg
)
char *p; /* pointer to traverse string */
char *firstdig; /* pointer to first digit */
char temp; /* temp char */
unsigned digval; /* value of digit */
p = buf;
if (is_neg)
/* negative, so output '-' and negate */
*p++ = '-';
val = (unsigned long)(-(long)val);
firstdig = p; /* save pointer to first digit */
do
digval = (unsigned) (val % radix);
val /= radix; /* get next digit */
/* convert to ascii and store */
if (digval > 9)
*p++ = (char) (digval - 10 + 'a'); /* a letter */
else
*p++ = (char) (digval + '0'); /* a digit */
while (val > 0);
/* We now have the digit of the number in the buffer, but in reverse
order. Thus we reverse them now. */
*p-- = '\0'; /* terminate string; p points to last digit */
do
temp = *p;
*p = *firstdig;
*firstdig = temp; /* swap *p and *firstdig */
--p;
++firstdig; /* advance to next two digits */
while (firstdig < p); /* repeat until halfway */
/* Actual functions just call conversion helper with neg flag set correctly,
and return pointer to buffer. */
char * __cdecl _itoa (
int val,
char *buf,
int radix
)
if (radix == 10 && val < 0)
xtoa((unsigned long)val, buf, radix, 1);
else
xtoa((unsigned long)(unsigned int)val, buf, radix, 0);
return buf;
char * __cdecl _ltoa (
long val,
char *buf,
int radix
)
xtoa((unsigned long)val, buf, radix, (radix == 10 && val < 0));
return buf;
char * __cdecl _ultoa (
unsigned long val,
char *buf,
int radix
)
xtoa(val, buf, radix, 0);
return buf;
#ifndef _NO_INT64
static void __stdcall x64toa ( /* stdcall is faster and smaller... Might as well use it for the helper. */
unsigned __int64 val,
char *buf,
unsigned radix,
int is_neg
)
char *p; /* pointer to traverse string */
char *firstdig; /* pointer to first digit */
char temp; /* temp char */
unsigned digval; /* value of digit */
p = buf;
if ( is_neg )
*p++ = '-'; /* negative, so output '-' and negate */
val = (unsigned __int64)(-(__int64)val);
firstdig = p; /* save pointer to first digit */
do
digval = (unsigned) (val % radix);
val /= radix; /* get next digit */
/* convert to ascii and store */
if (digval > 9)
*p++ = (char) (digval - 10 + 'a'); /* a letter */
else
*p++ = (char) (digval + '0'); /* a digit */
while (val > 0);
/* We now have the digit of the number in the buffer, but in reverse
order. Thus we reverse them now. */
*p-- = '\0'; /* terminate string; p points to last digit */
do
temp = *p;
*p = *firstdig;
*firstdig = temp; /* swap *p and *firstdig */
--p;
++firstdig; /* advance to next two digits */
while (firstdig < p); /* repeat until halfway */
/* Actual functions just call conversion helper with neg flag set correctly,
and return pointer to buffer. */
char * __cdecl _i64toa (
__int64 val,
char *buf,
int radix
)
x64toa((unsigned __int64)val, buf, radix, (radix == 10 && val < 0));
return buf;
char * __cdecl _ui64toa (
unsigned __int64 val,
char *buf,
int radix
)
x64toa(val, buf, radix, 0);
return buf;
#endif /* _NO_INT64 */
/////////////////////////////
例:
#include <stdio.h>
void main( void )
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;
_itoa( i, buffer, 10 );
printf( "String of integer %d (radix 10): %s\n", i, buffer );
_itoa( i, buffer, 16 );
printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
_itoa( i, buffer, 2 );
printf( "String of integer %d (radix 2): %s\n", i, buffer );
_ltoa( l, buffer, 16 );
printf( "String of long int %ld (radix 16): 0x%s\n", l,
buffer );
_ultoa( ul, buffer, 16 );
printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,
buffer );
LZ给分 参考技术B 用递归
#include
"stdio.h"
int
output(int
num);
void
main()
int
num;
printf("输入正整数:");
//当超过一定位数时,会溢出
scanf("%d",&num);
if(num<=0)
printf("输入整数!");
else
output(num);
int
output(int
num)
int
temp;
if(num!=0)
temp=num%2;
num=num/2;
output(num);
printf("%d",temp);
参考技术C 用 itoa()函数也可以实现这个功能
#include <stdlib.h>
int main()
int shu = 12;
char str[32];
itoa(shu, str, 2);
printf("integer = %d erjinzhi = %s\n", shu, str);
getch();
蓝桥杯 十六进制转八进制(超大测试数据,java实现)
问题描述
给定n个十六进制正整数,输出它们对应的八进制数。
输入格式
输入的第一行为一个正整数n (1<=n<=10)。
接下来n行,每行一个由0~9、大写字母A~F组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过100000。
输出格式
输出n行,每行为输入对应的八进制正整数。
【注意】
输入的十六进制数不会有前导0,比如012A。
输出的八进制数也不能有前导0。
样例输入
2
39
123ABC
样例输出
71
4435274
拿到题目觉着还挺简单,直接用了String和Integer的几个函数,样例输出没问题。提交上去发现提示运行错误。
下载下来测试数据,才知道输入进去了一个多么变态的数:
1 import java.util.Arrays; 2 import java.util.Scanner; 3 4 public class Main { 5 6 public static void main(String[] args) { 7 Scanner in=new Scanner(System.in); 8 int n=in.nextInt(); 9 String [] result=new String[11]; 10 for(int i=0;i<n;i++){ 11 String h=in.next(); 12 String b=Integer.toBinaryString(Integer.valueOf(h, 16)); 13 String o=Integer.toOctalString(Integer.valueOf(b, 2)); 14 result[i]=o; 15 } 16 for(int i=0;i<n;i++){ 17 System.out.println(result[i]); 18 } 19 } 20 } 21
测试数据:
在第一次的代码上做了改动:
①首先将每一位十六进制数转换为四位二进制数(一定要转为4位,使用前导0),保存为字符串;
②将字符串长度化为3的倍数,以便向八进制转化;
③每三位转化为八进制,去掉前导零;
代码如下,可通过测试:
1 import java.util.Arrays; 2 import java.util.Scanner; 3 4 public class Poj { 5 6 public static void main(String[] args) { 7 Scanner in=new Scanner(System.in); 8 int n=in.nextInt(); 9 String [] result=new String[11];//保存最后输出的结果 10 /*十六进制转化为二进制*/ 11 for(int i=0;i<n;i++){ 12 String h=in.next(); 13 StringBuilder tempB=new StringBuilder(); 14 for(int m=0;m<h.length();m++){ 15 char numH=h.charAt(m); 16 String b=Integer.toBinaryString(Integer.valueOf(String.valueOf(numH), 16)); 17 18 for(int k=b.length();k<4;k++){ 19 b=‘0‘+b; 20 } 21 22 tempB.append(b); 23 } 24 25 /*二进制转化为八进制*/ 26 StringBuilder tempO=new StringBuilder();//长度变为3的倍数,需要补的前导0的个数 27 int addZero=3-tempB.length()%3; 28 for(int p=0;p<addZero;p++){ 29 tempB=new StringBuilder("0").append(tempB); 30 } 31 for(int m=0;m<tempB.length();m+=3){ 32 //把字符串长度转换为三的倍数添加前导0 33 String numB=tempB.substring(m, m+3); 34 String o=Integer.toOctalString(Integer.valueOf(String.valueOf(numB), 2)); 35 tempO.append(o); 36 } 37 result[i]=tempO.toString().replaceAll("^(0+)", "");//用正则表达式去掉前导零 38 } 39 for(int i=0;i<n;i++){ 40 System.out.println(result[i]); 41 } 42 } 43 }
以上是关于C编程 实现输入一个正整数,输出其对应的二进制数。的主要内容,如果未能解决你的问题,请参考以下文章
将十进制数转换为二进制数----不用数组,也不用函数,只用循环
C语言:输入一个十六进制字符串,将其转换成为对应的整数并输出转换结果。