概述
#include <stdio.h>
void toBinary(int num);
void toOct(int num);
void toHex(int num);
void total(int num, int base, int offset);
void toBinary2(int num);
void toOct2(int num);
void toHex2(int num);
int main(int argc, const char * argv[])
{
toBinary(12);
toOct(12);
toHex(12);
toBinary2(12);
toOct2(12);
toHex2(12);
return 0;
}
void toBinary2(int num){
total(num, 1, 1);
}
void toOct2(int num){
total(num, 7, 3);
}
void toHex2(int num){
total(num, 15, 4);
}
void total(int num, int base, int offset)
{
char cs[16] = {
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
};
char rs[32];
int len = sizeof(rs) / sizeof(char);
int pos = len;
while (num) {
int result = num & base;
rs[--pos] = cs[result];
num = num>>offset;
}
for (int i = pos; i<len; i++) {
printf("%c", rs[i]);
}
printf("n");
}
// 转二进制
void toBinary(int num)
{
char cs[2] = {'0','1'};
char rs[32];
int len = sizeof(rs) / sizeof(char);
int pos = len; // 32
while (num != 0) {
int result = num & 1;
rs[--pos] = cs[result];
num = num>>1;
}
for (int i = pos; i<len; i++) {
printf("%c", rs[i]);
}
printf("n");
}
// 转八进制
void toOct(int num)
{
char cs[8] = {
'0','1','2','3','4','5','6','7'
};
char rs[11];
int len = sizeof(rs) / sizeof(char);
int pos = len; // 11;
while (num != 0) {
int result = num & 7;
rs[--pos] = cs[result];
num = num>>3;
}
for (int i = pos; i<len; i++) {
printf("%c", rs[i]);
}
printf("n");
}
// 转十六进制
void toHex(int num)
{
char cs[16] = {
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
};
char rs[8];
int len = sizeof(rs) / sizeof(char);
int pos = len;
while (num != 0) {
int result = num & 15;
rs[--pos] = cs[result];
num = num>>4;
}
for (int i = pos; i<len; i++) {
printf("%c", rs[i]);
}
printf("n");
}
i
最后
以上就是凶狠大地为你收集整理的int型转二进制,八进制,十六进制查表法的全部内容,希望文章能够帮你解决int型转二进制,八进制,十六进制查表法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复