十进制转16进制 、十进制转二进制 函数
include "string.h"
include "stdio.h"
char *dectohex(int dec, int len)
{
static char buf[256];
char cell[] = "0123456789ABCDEF";
int i = 0;
memset(buf, 0, 256);
memset(buf, '0', len);
while (dec != 0)
{
buf[i++] = cell[dec % 16];
dec = dec / 16;
}
strcat(buf, "x0");
return (strrev(buf));
}
char *dectobin(int dec, int len)
{
int i = 0;
static char buf[256];
memset(buf, 0, 256);
memset(buf, '0', len);
while (dec != 0)
{
buf[i++] = dec % 2+48;
dec = dec / 2;
}
return strrev(buf);
}
本文提供了一种将十进制数转换为十六进制和二进制字符串的方法。通过两个函数dectohex和dectobin实现转换过程,并使用字符数组进行存储与操作。
6239

被折叠的 条评论
为什么被折叠?



