今天去一公司面试、仍我给我一个用C#写的字符串转换方法、让我用Delphi照着去用Delphi写出来这个方法、我那叫一个哭。写了半截不行了。谁帮我看一下。C#的public float GO(string strBuffer)
{
char[] buffer;
float minusFlag, pointPos;
float nResult; buffer = strBuffer.ToCharArray(); // '检测标识位
int nState = buffer[1];
// '重量溢出
if ((nState & 0x80) > 0)
{
return 10002;
} //重量不稳定
if ((nState & 0x40) == 0)
{
return 10003;
} //负数
if ((nState & 0x20) == 0)
{
minusFlag = 1;
}
else
{
minusFlag = -1;
}
//非计重模式
if ((nState & 0xFF) == 0)
{
return 10004;
} //小数点位置
pointPos = nState % 8; // 循环取数值
nResult = 0;
for (int i = buffer.Length - 1; i >= 2; i--)
{
float nTemp = buffer[i];
nResult = nResult * 100 + Convert.ToInt32(nTemp / 16) * 10 + (nTemp % 16);
}
// '根据小数点位置处理数值
for (int i = 1; i < pointPos; i++)
{
nResult = nResult / 10;
}
// '加正负号
nResult = nResult * minusFlag; return nResult;
}
{
char[] buffer;
float minusFlag, pointPos;
float nResult; buffer = strBuffer.ToCharArray(); // '检测标识位
int nState = buffer[1];
// '重量溢出
if ((nState & 0x80) > 0)
{
return 10002;
} //重量不稳定
if ((nState & 0x40) == 0)
{
return 10003;
} //负数
if ((nState & 0x20) == 0)
{
minusFlag = 1;
}
else
{
minusFlag = -1;
}
//非计重模式
if ((nState & 0xFF) == 0)
{
return 10004;
} //小数点位置
pointPos = nState % 8; // 循环取数值
nResult = 0;
for (int i = buffer.Length - 1; i >= 2; i--)
{
float nTemp = buffer[i];
nResult = nResult * 100 + Convert.ToInt32(nTemp / 16) * 10 + (nTemp % 16);
}
// '根据小数点位置处理数值
for (int i = 1; i < pointPos; i++)
{
nResult = nResult / 10;
}
// '加正负号
nResult = nResult * minusFlag; return nResult;
}
buffer = strBuffer.ToCharArray();
你只要转换成char数组就行了,delphi中用move即可实现
S1:string; strbuffer:Array of char;
minusFlag, pointPos:double;
nResult: double;
nTemp:double;
nState:Integer;
i:Integer;begin
S1:=pchar('D');//S1直接当成参数来处理。
StrPCopy(@S1,pchar('D'));
// '检测标识位
nState:=Ord(S1[1]); //
//重量溢出
if((nState and $80)>0)then
begin
nResult:=10002;
end;
//重量不稳定
if((nState and $40)=0)then
begin
nResult:=10003;
end;
//负数
if((nState and $20)= 0)then
begin
minusFlag:=1;
end
else
begin
minusFlag := -1;
end;
//非记重模式
if ((nState and $FF)= 0)then
begin
nResult:=10004;
end;
//小数点位置
pointPos:=nState mod 8;
//循环取数值
nResult:=0; for i := Length(Copy(S1,0,100))-1 downto 2 do
begin
nTemp:=HexstrToInt(S1[i]);
nResult := nResult * 100+ round(nTemp / 16) * 10 + (round(nTemp) mod 16);
end; for i := 1 to trunc(pointPos) do
begin
nResult := nResult / 10;
end;
nResult := nResult * minusFlag;
edit3.Text:=(floattostr(nResult));
end;