Z
zhihuali
Unregistered / Unconfirmed
GUEST, unregistred user!
将下面TC2.0的函数翻译成 DELPHI函数,(打印函数可以不翻译)
如果正确,输入字符串StrToFloat('4140DE9E')后,
返回的是single值12.1
long HStrToDec(char *strHex)
{
int i,len;
long sum=0;
len=strlen(strHex);
if(len>8) return 0;
for(i=0;i<len;i++)
{
if(strHex>='0' &&
strHex<='9') strHex-=0x30;
else
{
strHex=toupper(strHex);
switch(strHex)
{
case 'A': strHex=0xa;break;
case 'B': strHex=0xb;break;
case 'C': strHex=0xc;break;
case 'D': strHex=0xd;break;
case 'E': strHex=0xe;break;
case 'F': strHex=0xf;break;
default: strHex=0x0;break;
}
}
sum+=(long)strHex<<(4*(len-1-i));
}
return sum;
}
float StrToFloat(char *buf)
{
long ptvalue,m,n;
int k;
float kf,tkf,s;
int flag=1;
char ptbuf[10];
char tmpbuf[10];
memset(ptbuf,0,sizeof(ptbuf)-1);
memset(tmpbuf,0,sizeof(tmpbuf)-1);
if(buf[0]=='B') { flag=-1;buf[0]='3';}
else
if(buf[0]=='C') { flag=-1;buf[0]='4';}
strncpy(ptbuf,buf,8);ptbuf[8]=0;//printf("/nptbuf:%s ",ptbuf);
strcpy(tmpbuf,ptbuf+1);tmpbuf[7]=0;
ptvalue=HStrToDec(ptbuf);//printf(" ptvalue: %lx ",ptvalue);
m=(long)HStrToDec(tmpbuf);
n=m/0x800000L;
if(ptvalue<0x40000000L) k=n-31;
else
k=n+1;
kf=(float)pow(2.0,k);
tkf=kf;
kf=kf/0x800000L;
s=tkf+(m-n*0x800000L)*kf;
s=s*flag;
//free(ptbuf);
//free(tmpbuf);
if(ptvalue==0) s=0;
printf("strValue=%lX,float=%f/n",ptvalue,s);
return s;
}
如果正确,输入字符串StrToFloat('4140DE9E')后,
返回的是single值12.1
long HStrToDec(char *strHex)
{
int i,len;
long sum=0;
len=strlen(strHex);
if(len>8) return 0;
for(i=0;i<len;i++)
{
if(strHex>='0' &&
strHex<='9') strHex-=0x30;
else
{
strHex=toupper(strHex);
switch(strHex)
{
case 'A': strHex=0xa;break;
case 'B': strHex=0xb;break;
case 'C': strHex=0xc;break;
case 'D': strHex=0xd;break;
case 'E': strHex=0xe;break;
case 'F': strHex=0xf;break;
default: strHex=0x0;break;
}
}
sum+=(long)strHex<<(4*(len-1-i));
}
return sum;
}
float StrToFloat(char *buf)
{
long ptvalue,m,n;
int k;
float kf,tkf,s;
int flag=1;
char ptbuf[10];
char tmpbuf[10];
memset(ptbuf,0,sizeof(ptbuf)-1);
memset(tmpbuf,0,sizeof(tmpbuf)-1);
if(buf[0]=='B') { flag=-1;buf[0]='3';}
else
if(buf[0]=='C') { flag=-1;buf[0]='4';}
strncpy(ptbuf,buf,8);ptbuf[8]=0;//printf("/nptbuf:%s ",ptbuf);
strcpy(tmpbuf,ptbuf+1);tmpbuf[7]=0;
ptvalue=HStrToDec(ptbuf);//printf(" ptvalue: %lx ",ptvalue);
m=(long)HStrToDec(tmpbuf);
n=m/0x800000L;
if(ptvalue<0x40000000L) k=n-31;
else
k=n+1;
kf=(float)pow(2.0,k);
tkf=kf;
kf=kf/0x800000L;
s=tkf+(m-n*0x800000L)*kf;
s=s*flag;
//free(ptbuf);
//free(tmpbuf);
if(ptvalue==0) s=0;
printf("strValue=%lX,float=%f/n",ptvalue,s);
return s;
}