Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi C++ pointer to byte array --> Delphi (https://www.delphipraxis.net/177855-c-pointer-byte-array-delphi.html)

WojTec 1. Dez 2013 18:47

Delphi-Version: 2010

C++ pointer to byte array --> Delphi
 
There is definition in function header:

Code:
unsigned char *k
Then inside while loop there is:

Code:
a += ( k[0] + ( (unsigned)k[1] << 8 )) .........
k += 12;
If k is increased, then what mean k[%d] in every iteration? Isn't it first byte from data, but first byte from every 12 bytes package, I think?

In Delphi should be:

Code:
v := 0;
k[v + 1], +2, etc
inc(v, 12);
?

And what mean typecasting to unsigned, when char in C++ is just byte?

Der schöne Günther 1. Dez 2013 20:02

AW: C++ pointer to byte array --> Delphi
 
This is sorcery!

Seriously, this is C at its finest not really.

As far as I can see, the upcast to
Delphi-Quellcode:
unsigned
happens because the
Delphi-Quellcode:
byte
value is leftshifted by eight! That means it's multiplied by 2^8 = 256. As far as I can remember, just
Delphi-Quellcode:
unsigned
means
Delphi-Quellcode:
unsigned int
. Only this way, you're able to store the maximum result value of up to 65535.

This way, your first line means "increase a by the sum of k[0] and k[1]*2^8".

Pointer arithmetics work just like in Delphi: If you increase your
Delphi-Quellcode:
unsigned char
pointer by 12, it's now pointing at the next address 12 bytes "later". In case it had been a pointer to short, it would now have been moved by 24 bytes.

I still don't have the slightest clue what and why is actually happening in this code, but I guess I at least figured out those two lines ;-)

Mikkey 2. Dez 2013 06:42

AW: C++ pointer to byte array --> Delphi
 
In C Pointer and Array are nearly the same.

So k[0] and *k lead to the same expression that is the character at the place where k points to.

char in C is defined as signed, so unsigned makes sense.

Translation to Delphi could be:

Delphi-Quellcode:
var v: Integer; {k: array of Shortint;}
begin
  v := 0;
  a := a + k(v] + 256*Byte(k[v+1]);
  v := v + 12;
end;

WojTec 2. Dez 2013 11:55

Re: C++ pointer to byte array --> Delphi
 
Let's check original C and C# code:

Code:
unsigned jen_hash ( unsigned char *k, unsigned length, unsigned initval )
{
   unsigned a, b;
   unsigned c = initval;
   unsigned len = length;

   a = b = 0x9e3779b9;

   while ( len >= 12 ) {
      a += ( k[0] + ( (unsigned)k[1] << 8 )
        + ( (unsigned)k[2] << 16 )
        + ( (unsigned)k[3] << 24 ) );
      b += ( k[4] + ( (unsigned)k[5] << 8 )
        + ( (unsigned)k[6] << 16 )
        + ( (unsigned)k[7] << 24 ) );
      c += ( k[8] + ( (unsigned)k[9] << 8 )
        + ( (unsigned)k[10] << 16 )
        + ( (unsigned)k[11] << 24 ) );

      mix ( a, b, c ); // this is macro with basic bit math

      k += 12;
      len -= 12;
   }

   c += length;

   switch ( len ) {
   case 11: c += ( (unsigned)k[10] << 24 );
   case 10: c += ( (unsigned)k[9] << 16 );
   case 9 : c += ( (unsigned)k[8] << 8 );
   case 8 : b += ( (unsigned)k[7] << 24 );
   case 7 : b += ( (unsigned)k[6] << 16 );
   case 6 : b += ( (unsigned)k[5] << 8 );
   case 5 : b += k[4];
   case 4 : a += ( (unsigned)k[3] << 24 );
   case 3 : a += ( (unsigned)k[2] << 16 );
   case 2 : a += ( (unsigned)k[1] << 8 );
   case 1 : a += k[0];
   }

   mix ( a, b, c );

   return c;
}
Input is string. I made:

Delphi-Quellcode:
  while L >= 12 do
  begin
    Inc(A, (Ord(AValue[K + 1]) + (Ord(AValue[K + 2]) shl 8) + (Ord(AValue[K + 3]) shl 16) + (Ord(AValue[K + 4]) shl 24)));
    Inc(B, (Ord(AValue[K + 5]) + (Ord(AValue[K + 6]) shl 8) + (Ord(AValue[K + 7]) shl 16) + (Ord(AValue[K + 8]) shl 24)));
    Inc(C, (Ord(AValue[K + 9]) + (Ord(AValue[K + 10]) shl 8) + (Ord(AValue[K + 11]) shl 16) + (Ord(AValue[K + 12]) shl 24)));

    Mix(A, B, C);

    Inc(K, 12);
    Dec(L, 12);
  end;

  case L of
    11: Inc(C, Ord(AValue[11]) shl 24);
    10: Inc(C, Ord(AValue[10]) shl 16);
    9: Inc(C, Ord(AValue[9]) shl 8);
And another function:

Code:
public override HashResult ComputeBytes(byte[] a_data)
{
    int length = a_data.Length;

    if (length == 0)
        return new HashResult(0);

    uint hash = (UInt32)length;

    int currentIndex = 0;

    while (length >= 4)
    {
        hash += (ushort)(a_data[currentIndex++] | a_data[currentIndex++] << 8);
        uint tmp = (uint)((uint)(a_data[currentIndex++] | a_data[currentIndex++] << 8) << 11) ^ hash;
        hash = (hash << 16) ^ tmp;
        hash += hash >> 11;

        length -= 4;
    }

    switch (length)
    {
        case 3:
            hash += (ushort)(a_data[currentIndex++] | a_data[currentIndex++] << 8);
            hash ^= hash << 16;
            hash ^= ((uint)a_data[currentIndex]) << 18;
            hash += hash >> 11;
            break;
        case 2:
            hash += (ushort)(a_data[currentIndex++] | a_data[currentIndex] << 8);
            hash ^= hash << 11;
            hash += hash >> 17;
            break;
        case 1:
            hash += a_data[currentIndex];
            hash ^= hash << 10;
            hash += hash >> 1;
            break;
        default:
            break;
    }

    hash ^= hash << 3;
    hash += hash >> 5;
    hash ^= hash << 4;
    hash += hash >> 17;
    hash ^= hash << 25;
    hash += hash >> 6;

    return new HashResult(hash);
}
I made:

Delphi-Quellcode:
  I := 1;

  while L >= 4 do
  begin
    Inc(I);

    Result := Result + (Ord(AValue[I]) and Ord(AValue[I + 1]) shl 8);
    Result := (Result shl 16) xor (((Ord(AValue[I + 2]) and Ord(AValue[I + 3]) shl 8) shl 11) xor Result);
    Result := Result + (Result shr 11);

    Inc(I, 4);
    Dec(L, 4);
  end;

  case L of
    3:
    begin
      Result := Result + (Ord(AValue[I]) and Ord(AValue[I + 1]) shl 8);
      Result := Result xor (Result shl 16);
      Result := Result xor (Ord(AValue[I + 2]) shl 18);
      Result := Result + (Result shr 11);
    end;
I did oke or not? Must multiply by 256? I still don't understand why?

BTW: "|" mean "and" "or"?

Der schöne Günther 2. Dez 2013 11:59

AW: C++ pointer to byte array --> Delphi
 
I don't have the time to dig through your source code right now, but "A || B" means "A or B" whereas "A && B" means "A and B".

"A & B" means that B is evaluated, even if A is already False, same for "A | B". As far as I can remember, Delphi only supports forcefully evaluating the second expression by compiler switches. So pay extra attention if you ever see something like "A|B" or "A&B".

DeddyH 2. Dez 2013 12:08

AW: C++ pointer to byte array --> Delphi
 
| = or (binary), & = and (binary), ^ = xor.

[edit] Notice: there is no break at the end of the switch-cases in the first C-code. IIRC this means, all following cases will be processed as well. [/edit]

WojTec 2. Dez 2013 12:09

Re: C++ pointer to byte array --> Delphi
 
If I remember in C and similar || and && is Boolean arithmetic, but | and & is for bits. In Delphi and/or is applicateble for both (compiler decide real meaning), so "|" really mean "or" or need some additional tricks?
@DeddyH, you was first :D, thanks :)

So, my code is ok?

DeddyH 2. Dez 2013 12:12

AW: C++ pointer to byte array --> Delphi
 
Please have a look at the edit in my last post.

WojTec 2. Dez 2013 12:19

Re: C++ pointer to byte array --> Delphi
 
It mean if case 11 then process all, if 10 than all instead above, etc.?

And what about rest operations? Indexes are valid? 256 multiply is required?

DeddyH 2. Dez 2013 12:29

AW: C++ pointer to byte array --> Delphi
 
I am not that familiar with C/C++, sry.


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:43 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz