1. 程式人生 > >c語言-10進位制字串轉16進位制字串

c語言-10進位制字串轉16進位制字串

程式碼:

#include <stdio.h>
#include <string.h>
#include <malloc.h>
char *DecToHex(char *pHex,char *pDec,int Declen)
{
	int       i,Hexlen = Declen *2;
	char        hex[] = "0123456789";
	pHex = (char *)malloc(sizeof(char)*Hexlen);
	for (i = 0; i < Declen; i++)
	{
		*pHex++ = hex[*pDec>>4];
		*pHex++ = hex[*pDec++ & 0x0F];
	}

	return pHex -Hexlen;
}
int main()
{
	char Dec[] = "01234";
	char *pHex = NULL;
	printf("%s", DecToHex(pHex,Dec,strlen(Dec)));
	free(pHex);
}

執行結果: