求各位前辈:将一小段VB转换为delphi(50分)

  • 主题发起人 主题发起人 hangyang
  • 开始时间 开始时间
H

hangyang

Unregistered / Unconfirmed
GUEST, unregistred user!
Public Function hextodec(aa As String, mdlS As Integer, numS As Long) As Variant
Dim t As String, u As String
Dim h As Integer, numb As Long
numb = 0
t = Mid(aa, mdlS, numS)
For i = 1 To Len(t) / 2
numb = numb * 256 + Val("&h" &
(Mid(t, i * 2 - 1, 2)))
Next i
hextodec = numb
End Function
 
function _HexToInt(HexStr: String): Int64;
var
RetVar : Int64;
i : byte;
begin
if (HexStr='') then
begin
result := 0;
exit;
end;
HexStr := UpperCase(HexStr);
if HexStr[length(HexStr)] = 'H' then
Delete(HexStr,length(HexStr),1);
RetVar := 0;
for i := 1 to length(HexStr)do
begin
RetVar := RetVar shl 4;
if HexStr in ['0'..'9'] then
RetVar := RetVar + (byte(HexStr) - 48)
else
if HexStr in ['A'..'F'] then
RetVar := RetVar + (byte(HexStr) - 55)
else
begin
Retvar := 0;
break;
end;
end;
result := RetVar;
end;

function _StrToHex(HexStr: String): String;
var
i: integer;
begin
result := '';
for i := 1 to Length(HexStr)do
result := result + IntToHex( Ord( HexStr ), 2 );
end;

Function hextodec(aa :String;
mdlS,numS:Integer):Double;
var
t :String;
i :Integer;
numb :Double;
begin
numb := 0;
t:=Copy(aa,mdlS,numS);
For i := 1 to Trunc(Length(t)/2)do
numb := numb* 256 + _HexToInt(_StrToHex(Copy(t, i* 2-1,2)));
Result := numb
end;
 
完全按照LZ给出的VB代码翻译出来的是
function hextodec(aa : String;
mdlS : Integer;
numS : Integer):Variant;
var
s : String;
I : Integer;
begin
Result := 0;
s := Copy(aa, mdlS, numS);
for I := 1 to Length(s) div 2do
Result := Result*256 + StrToInt('$' + copy(s, I*2 - 1, 2));
end;
不过有点繁琐.有更简单的方法.
function hextodec(aa : String;
mdlS : Integer;
numS : Integer):Variant;
begin
Result := StrToInt('$' + Copy(aa, mdlS, numS));
end;
另外Delphi的字符串的第一个字符下标是1.注意一下就行了
 
感谢两位前辈!
 
后退
顶部