Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Delphi
  4. Delphi to C#

Delphi to C#

Scheduled Pinned Locked Moved Delphi
csharpdelphidata-structures
3 Posts 3 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    coert dorrepaal
    wrote on last edited by
    #1

    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;

    L S 2 Replies Last reply
    0
    • C coert dorrepaal

      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;

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C coert dorrepaal

        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;

        S Offline
        S Offline
        smags13
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups