编程问题:16进制出错在哪?只要出错处
//#include <creation-zero.h>
#include <iostream>
#include <stdio.h>
using namespace std;
char syston10(char c[],int a,int b,int m)
{
int i=0,n=0,l=1;
while(1)
{
if(c[i]==' ')
break;
if(c[i]>='0'&&c[i]<='9')
n+=(c[i]-'0')*l;
if(c[i]>='A'&&c[i]<='Z')
n+=(c[i]-'A'+10)*l;
if(c[i]>='a'&&c[i]<='z')
n+=(c[i]-'a'+10)*l;
i++;
l*=a;
}
int q=0,fuck=n;
while(1)
{
n/=b;
q++;
if(n==0)
break;
}
char p[q];
int u;
for(int z=q-1;z>=0;z++)
{
u=n%b;
n/=b;
if(u>=0&&u<=9)
p[z]=u+'0';
if(u>=10)
p[z]=u-10+'A';
}
if(m<q)
return p[m];
else
return -1;
}
int main()
{
char a[66666];
int m=0;
while(1)
{
scanf("%c",&a[m]);
if(a[m]==' ')
break;
m++;
}
int n=0;
while(1)
{
if(syston10(a,13,18,n)==-1)
break;
cout<<syston10(a,13,18,n);
n++;
}
}
根本没有看明白你的算法,在你的
char syston10(char c[],int a,int b,int m)
中,a/b是常量,分别是13和18.这个与进制转换有关?
另外,既然是常量,又何必作为参数,另外你的m也就是主函数的n值,它也是常量0没有意义
同样在函数中,明明可以直接输入字串(cin及scanf都可以)
你却要用getchar?
程序语法上没有任何错,但算法看上去完成不对
以下是我写的,将一个字串转成2~36进制的函数(我一直用存OJ中的)
void m_itoa( char *buf, int i, unsigned char base)
{
unsigned int rem;
unsigned char *s, length = 0;
s = ( unsigned char *)buf;
if(i == 0)
*s++ = '0';
else
{
if(i < 0)
{
if(base <= 10)
{
*buf++ = '-';
s =( unsigned char *) buf;
}
i = -i;
}
while(i)
{
++length;
rem = i % base;
if(rem <= 10)
*s++ = rem + '0';
else if(base <= 36)
*s++ = rem - 10 + 'A';
i /= base;
}
for(base = 0; base < length / 2; base++)
{
*(buf + length) = *(buf + base);
*(buf + base) = *(buf + (length - base - 1));
*(buf + (length - base - 1)) = *(buf + length);
}
}
*s = 0;
}
假设数据在A2则:B2=CHAR(MOD(J2,8)+LEFT(J2)*8+55-7*(J2%<10.1))&CHAR(MOD(I2,8)+LEFT(I2)*8+55-7*(I2%<10.1))&CHAR(MOD(H2,8)+LEFT(H2)*8+55-7*(H2%<10.1))