Delphi to C#
-
function TAclasencode.BufToDataBIN(const Buf: TByteDynArray; iStartPos, iLen, iDec: Integer): Variant; const DefaultLen = 8; var i : Integer; iTmp : Int64; arr : array[0..7]of Byte; pInt64 : ^Int64; fTmp : Double; begin fTmp := 0; try FillChar(arr,8,0); if (Buf[iStartPos] and $80=$80) and (not IsCardinal) then begin if Negative then begin for I:=0 to iLen-2 do arr[I]:=(Buf[iStartPos+iLen-1-I] xor $FF); pInt64:=@arr[0]; iTmp:=pInt64^+1; end else begin for I:=0 to iLen-2 do arr[I]:=(Buf[iStartPos+iLen-1-I]); pInt64:=@arr[0]; iTmp:=pInt64^; end; fTmp:=iTmp; fTmp:=fTmp/Power(10,iDec); fTmp:=0-fTmp; end else begin for I:=0 to iLen-1 do arr[I]:=(Buf[iStartPos+iLen-1-I]); pInt64:=@arr[0]; iTmp:=pInt64^; fTmp:=iTmp; fTmp:=fTmp/Power(10,iDec); end; finally Result := fTmp; end; end;
-
function TAclasencode.BufToDataBIN(const Buf: TByteDynArray; iStartPos, iLen, iDec: Integer): Variant; const DefaultLen = 8; var i : Integer; iTmp : Int64; arr : array[0..7]of Byte; pInt64 : ^Int64; fTmp : Double; begin fTmp := 0; try FillChar(arr,8,0); if (Buf[iStartPos] and $80=$80) and (not IsCardinal) then begin if Negative then begin for I:=0 to iLen-2 do arr[I]:=(Buf[iStartPos+iLen-1-I] xor $FF); pInt64:=@arr[0]; iTmp:=pInt64^+1; end else begin for I:=0 to iLen-2 do arr[I]:=(Buf[iStartPos+iLen-1-I]); pInt64:=@arr[0]; iTmp:=pInt64^; end; fTmp:=iTmp; fTmp:=fTmp/Power(10,iDec); fTmp:=0-fTmp; end else begin for I:=0 to iLen-1 do arr[I]:=(Buf[iStartPos+iLen-1-I]); pInt64:=@arr[0]; iTmp:=pInt64^; fTmp:=iTmp; fTmp:=fTmp/Power(10,iDec); end; finally Result := fTmp; end; end;
Please edit the above code and put it between <pre> tags so it is readable. Also expalin what your problem is and what help you require.
Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness
-
function TAclasencode.BufToDataBIN(const Buf: TByteDynArray; iStartPos, iLen, iDec: Integer): Variant; const DefaultLen = 8; var i : Integer; iTmp : Int64; arr : array[0..7]of Byte; pInt64 : ^Int64; fTmp : Double; begin fTmp := 0; try FillChar(arr,8,0); if (Buf[iStartPos] and $80=$80) and (not IsCardinal) then begin if Negative then begin for I:=0 to iLen-2 do arr[I]:=(Buf[iStartPos+iLen-1-I] xor $FF); pInt64:=@arr[0]; iTmp:=pInt64^+1; end else begin for I:=0 to iLen-2 do arr[I]:=(Buf[iStartPos+iLen-1-I]); pInt64:=@arr[0]; iTmp:=pInt64^; end; fTmp:=iTmp; fTmp:=fTmp/Power(10,iDec); fTmp:=0-fTmp; end else begin for I:=0 to iLen-1 do arr[I]:=(Buf[iStartPos+iLen-1-I]); pInt64:=@arr[0]; iTmp:=pInt64^; fTmp:=iTmp; fTmp:=fTmp/Power(10,iDec); end; finally Result := fTmp; end; end;
Ha, I wouldn't grab your thought if I didn't come across your post in Quick Answer section. Please edit your post here so that other people would understand. Ok, first the function might have overflow issue, depending on the logic, and it is not exception safe, leading to unexpected results. But it's nothing to do with your question, so I will pass this part. I assume 1. TByteDynArray is array of Byte. if it's not, the following might be useless. :sigh: 2. and endian representation is the same on all machines where your codes will run.
double BufToDataBin(byte[] Buf, int iStartPos, int iLen, int iDec)
{
const int DefaultLen = 8;
double fTmp = 0;
Int64 iTmp = 0;
try
{
byte[] arr = { 0, 0, 0, 0, 0, 0, 0, 0 };
if ((Buf[iStartPos] & 0x80) == 0x80 && !IsCardinal)//IsCardinal??
{
if (Negative)//Negative??
{
for (int i = 0; i <= iLen - 2; ++i)
{
arr[i] = (byte)(Buf[iStartPos + iLen - 1 - i] ^ 0xFF);
}
iTmp = BitConverter.ToInt64(arr, 0);
}
else
{
for (int i = 0; i <= iLen - 2; ++i)
{
arr[i] = (byte)(Buf[iStartPos+iLen-1-i]);
}
iTmp = BitConverter.ToInt64(arr, 0);
}
fTmp = 0 - iTmp / Math.Pow(10, iDec);} else { for (int i = 0; i < iLen; ++i) { arr\[i\] = Buf\[iStartPos+iLen-1-i\]; } iTmp = BitConverter.ToInt64(arr, 0); fTmp = iTmp / Math.Pow(10, iDec); }
}
catch
{
//do something
}
return fTmp;
}Further optimization might be requested.