编写一个函数,能把2进制数,10进制数,16进制数相互转换,用c语言写。

不知道十六进制咋转换,然后还有函数咋写,不知道


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned char uint8_t;
typedef unsigned long uint64_t;

//求数的二进制最高位的幂指数,即MSB
static size_t getPolynomialBits(uint64_t n) {
    size_t r = 0;
    while (n) {
	    n >>= 1;
    	r++;
    }
    return r;
}

//十进制转二进制字符串,Flag表示是否在前置位补0为标准形式
int dec2BinaryString(uint64_t inputDec, unsigned char* binary, int Flag) 
{
    size_t length = getPolynomialBits(inputDec + 1) + 1; // 二进制表示的最少位数,如十进制15至少需要4位二进制位表示
	
    size_t padNum = (length%4 ? (4-length%4) : 0); // 高位补0数
	size_t resultLen = length + padNum ;
	int index = 0;
	int tmp = 0;

	unsigned char* result = (unsigned char*)malloc(sizeof(unsigned char) * ( resultLen + 1));
	memset(result, 0, sizeof(unsigned char) * (resultLen)); //初始化为0
	result[resultLen] = '\0';
	while (inputDec) {
		tmp = inputDec % 2; //除2取余
		index++;
		if (tmp==1) {
		//从低位往高位赋值
			result[resultLen - index] = 1;
		}
		else {
			result[resultLen - index] = 0;
		}
		inputDec >>= 1;
	}
	// 如果要输出为二进制的标准形式(4的倍数)字符串
	if (Flag) {
        //will to be fill zero in highest bit
        memcpy(binary, result, sizeof(unsigned char) * resultLen);
	}
	else {
		memcpy(binStream, result, sizeof(unsigned char) * length);
	}
    free(result);
    result = NULL;
    return 0;
}

static uint64_t Binary2Decimal(uint8_t* inputBinary, unsigned int lenBinBits)
{
	if (NULL == inputBinary)
		return 0;

	uint64_t nDecimal = 0;
    uint64_t sum = 0;
	for (uint32_t i = 0; i < lenBinBits; i++)
	{
		nDecimal = inputBinary[i] & 1; // '0' convert 0
		nDecimal <<= (lenBinBits - 1 - i);
        sum += nDecimal;
	}
	return sum;
}

//十进制转十六进制字符串
//decimal number convert to hex string
//参数length表示存储十进制字符串的最大长度
int dec2HexStream(uint64_t inputValue, unsigned int length, char* resStream) {

	uint64_t inputDecimal = inputValue;
	char* result = ( char*)malloc(sizeof( char) * (length + 1));
	//will to be fill zero in highest bit
	memset(result, '0', sizeof( char) * length);
	
	int msbIdx = 0;
	int tmp = 0;
	while (inputDecimal) {
		tmp = inputDecimal % 16;
		msbIdx++;
		if (tmp <= 9) {
			result[length - msbIdx] = tmp + '0';
		}
		else {
			result[length - msbIdx] = tmp - 10 + 'A';
		}
		inputDecimal >>= 4;
	}
	result[length] = '\0';
	

	if (msbIdx <= length) {
		memcpy(resStream, result, sizeof(char) * length);
		free(result);
		return 0;
	}
	else {
		printf("\n input parameter length less more msbIdx's length, %d < %d \n", length, msbIdx);
		free(result);
		return -1;
	}
}

//二进制字符串转16进制数字符串
int bin2Hex(const char* inputBin, uint64_t& decValue, char* hexStr) {
	int inputLen = strlen(inputBin);
	if (inputBin == NULL || inputLen == 0) {
		return -1;
	}

	unsigned int n = (inputLen % 4 ? (4 - inputLen % 4):0); //16进制高位补0数
	unsigned int idx = (inputLen + n) / 4; // 16进制位数

	char* result = (char*)calloc(idx + 1, sizeof(char));

	char* msg = (char*)calloc(inputLen + n , sizeof(char));
	memset(msg, '0', sizeof(char)*n);
	memcpy(msg + sizeof(char) * n, inputBin, sizeof(char) * inputLen);
	
	uint64_t dec = 0;
	char tmp[4] = { '0' };
	for (int k = 0; k < idx; k ++) {
		memcpy(tmp, msg + k*4, 4);
		uint64_t res = Bin2Decimal(tmp, 4);
		
		if (res >= 0 && res <= 9) {
			result[k] = res + '0';
		}
		if (res > 9 && res <= 15) {
			result[k] = res  + ('A'- 10);
		}
		dec += res * pow(16, idx-1 - k);
	}
	
	memcpy(hexStr, result, idx);
	decValue = dec;
	free(msg);
	free(result);
	return 0;
}

 

只写了一个从10进制分别转换成2,8,16进制的,请参考。 

#include<stdio.h>
#include<stdlib.h>

int baseConversion(int num,int base)
{
	if (base==2)
	{
		char s[100]; //自定义二进制数的位数,输出位数是实际所需位数
		itoa(num,s,2);//<stdlib.h>库函数,转成字符串,基数为2
		printf("二进制:%s\n",s);
	}
	else if(base==8)
	{
		printf("八进制:%o\n",num);
	}
	else if(base==16)
	{
		printf("十六进制:%#x\n",num);
	}
	else
	{
		printf("your base is out of range.");
	}
	return 0;
}


int main(){
	int num;
	int base;
	printf("please input your number:");
	scanf("%d",&num);
	printf("please input your base number:");
	scanf("%d",&base);
	baseConversion(num,base);
	
	return 0;
}