学弟教程-十进制转其他进制

文章目录

1 C/C++

使用自带函数

itoa(num,str,n);
  • num 十进制数
  • str char数组,用来存储结果
  • n 代表准备转换的进制

int num  = 15;

char str[30];

//将15转为2进制
itoa(num , str, 2);

//将15转为16进制
itoa(num , str, 16);

完整代码

#include<stdio.h>
#include<stdlib.h>
//注意必须调用stdlib.h函数库
int main(void) {
	int a;
	scanf("%d",&a);
	char str[30];
	
	itoa(a,str,2);
	printf("%s\n",str);

	itoa(a,str,16);
	printf("%s\n",str);
	return 0;
}

运行结果

2 Python

使用自带函数 bin(),oct(),hex()
示例代码

x = 15

print("{0}的二进制值为:{1}".format(x, bin(x)))

print("{0}的八进制值为:{1}".format(x, oct(x)))

print("{0}的十六进制值为:{1}".format(x, hex(x)))


猜你喜欢

转载自blog.csdn.net/qq_41452937/article/details/106963182