AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

C++ pointer to byte array --> Delphi

Ein Thema von WojTec · begonnen am 1. Dez 2013 · letzter Beitrag vom 2. Dez 2013
Antwort Antwort
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

Re: C++ pointer to byte array --> Delphi

  Alt 2. Dez 2013, 11:55
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"?

Geändert von WojTec ( 2. Dez 2013 um 12:03 Uhr) Grund: Mistake in btw part
  Mit Zitat antworten Zitat
Der schöne Günther

Registriert seit: 6. Mär 2013
6.114 Beiträge
 
Delphi 10 Seattle Enterprise
 
#2

AW: C++ pointer to byte array --> Delphi

  Alt 2. Dez 2013, 11:59
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".
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#3

Re: C++ pointer to byte array --> Delphi

  Alt 2. Dez 2013, 12:09
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 , thanks

So, my code is ok?
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.546 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: C++ pointer to byte array --> Delphi

  Alt 2. Dez 2013, 12:12
Please have a look at the edit in my last post.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#5

Re: C++ pointer to byte array --> Delphi

  Alt 2. Dez 2013, 12:19
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?
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.546 Beiträge
 
Delphi 11 Alexandria
 
#6

AW: C++ pointer to byte array --> Delphi

  Alt 2. Dez 2013, 12:29
I am not that familiar with C/C++, sry.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Namenloser

Registriert seit: 7. Jun 2006
Ort: Karlsruhe
3.724 Beiträge
 
FreePascal / Lazarus
 
#7

AW: C++ pointer to byte array --> Delphi

  Alt 2. Dez 2013, 12:53
Looks mostly good to me, but I would pay some extra attention to the type casts. I am not sure if ord() always gives the correct result here. In some places the type cast might be done to truncate a value. For example, (ushort)foo gives us the lowest 16 bits of foo .

And in the top code, I think you can get rid of ord() entirely, if you've correctly translated char as byte.

Geändert von Namenloser ( 2. Dez 2013 um 12:55 Uhr)
  Mit Zitat antworten Zitat
Mikkey

Registriert seit: 5. Aug 2013
265 Beiträge
 
#8

AW: Re: C++ pointer to byte array --> Delphi

  Alt 2. Dez 2013, 12:51
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?
Don't worry with 256: Multiplying with 256 is the same as shifting left 8 bits.

You must invent some construct for the C-Case, bcse the Delphi case has implicit C-"break"s. Perhaps like:

Delphi-Quellcode:
if length = 11 then ...
if length >= 10 then ...
if length >= 9 then ...
...
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.546 Beiträge
 
Delphi 11 Alexandria
 
#9

AW: C++ pointer to byte array --> Delphi

  Alt 2. Dez 2013, 12:08
| = 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]
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen

Geändert von DeddyH ( 2. Dez 2013 um 12:10 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:44 Uhr.
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