大数据进制转换,先转出十六进制(参考网上例子),再转成二进制,再转成32进制;
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
const char s32bit_const[32] = {"0123456789abcdefghijklmnopqrstuv"};
char* strrev(char* s)
{
/* h指向s的头部 */
char* h = s;
char* t = s;
char ch;
/* t指向s的尾部 */
while(*t++){};
t--; /* 与t++抵消 */
t--; /* 回跳过结束符'\0' */
/* 当h和t未重合时,交换它们所指向的字符 */
while(h < t)
{
ch = *h;
*h++ = *t; /* h向尾部移动 */
*t-- = ch; /* t向头部移动 */
}
return(s);
}
/*
* dec 十进制数组
* _s32bit 32进制
*/
int hexchangeto32bit(char* dec,char* _s32bit)
{
int i,j,k,a[110],hex[110],t;
int flag;
strrev(dec);
for(i=0;i<110;i++)
a[i]=hex[i]=0;
for(i=0;dec[i];i++)
a[i]=dec[i]-48;//将字符转化为数字
//十进制转十六进制
for(t=0;t<strlen(dec);t++)
{
for(i=0;i<strlen(dec);i++)
{
a[i]*=625;
}
for(i=0;i<strlen(dec);i++)
{
if(a[i]>9)
{
a[i+1]+=a[i]/10;
a[i]%=10;
}
}
hex[t]=(a[0]+a[1]*10+a[2]*100+a[3]*1000)/625;//求余数
for(i=0;i<strlen(dec)+1;i++)
{
a[i]=a[i+4];//去掉余数
}
flag = 0;
for(i=0;i<strlen(dec)+1;i++)
{
if(0 < a[i])
flag = 1;
}
if(flag == 0)
break;
}
for(;!hex[t]&&t>0;t--);//去掉前导零
//十六进制转2进制
char bt[500];
memset(bt,0,sizeof(bt));
//1000 : hex=3e8; a0~hex(8) a4~hex(e)
for(i=0,j=0; j < t+1; j++)
{
if(hex[j] == 0)
{
i += 4;
continue;
}
else if(hex[j] <= 0x0f)
{
bt[i] = (hex[j]&0x01)? 1:0;
bt[i+1] = (hex[j]&0x02)? 1:0;
bt[i+2] = (hex[j]&0x04)? 1:0;
bt[i+3] = (hex[j]&0x08)? 1:0;
i +=4 ;
continue;
}
else
{
for(; 0 < hex[j]; i++)
{
bt[i] = hex[j]&0x01;
hex[j] >>= 1;
}
}
}
//2进制转32进制
int binlen=0;
unsigned char bin[200];
memset(bin,0,sizeof(bin));
for(j=0,k=0; i > 0 ;j++,i-=5,k+=5)
{
bin[j] |= bt[k]&0x01 ? 0x01:0;
bin[j] |= bt[k+1]&0x01 ? 0x02:0;
bin[j] |= bt[k+2]&0x01 ? 0x04:0;
bin[j] |= bt[k+3]&0x01 ? 0x08:0;
bin[j] |= bt[k+4]&0x01 ? 0x10:0;
binlen++;
}
for(;!bin[binlen]&&binlen>0;binlen--);//去掉前导零
for(i=0;i <= binlen ; i++)
{
_s32bit[i]=s32bit_const[bin[binlen - i]];
}
return binlen+1;
}
int main(int argc, char** argv)
{
int i;
char s[105];
unsigned char bin[500];
char s32bit[100];
int binlen;
while(gets(s),strcmp(s,"0"))
{
binlen = hexchangeto32bit(s,s32bit);
for(i=0;binlen > i;i++)
printf("%c ",s32bit[i]);
puts("\n");
}
}