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
Seite 1 von 2  1 2      
WojTec

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

C++ pointer to byte array --> Delphi

  Alt 1. Dez 2013, 19:47
Delphi-Version: 2010
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?

Geändert von WojTec ( 1. Dez 2013 um 19:51 Uhr)
  Mit Zitat antworten Zitat
Der schöne Günther
Online

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

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

  Alt 1. Dez 2013, 21:02
This is sorcery!

Seriously, this is C at its finest not really.

As far as I can see, the upcast to unsigned happens because the byte value is leftshifted by eight! That means it's multiplied by 2^8 = 256. As far as I can remember, just unsigned means 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 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
  Mit Zitat antworten Zitat
Mikkey

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

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

  Alt 2. Dez 2013, 07:42
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;
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#4

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

  Alt 2. Dez 2013, 12: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 13:03 Uhr) Grund: Mistake in btw part
  Mit Zitat antworten Zitat
Der schöne Günther
Online

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

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

  Alt 2. Dez 2013, 12: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
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.625 Beiträge
 
Delphi 12 Athens
 
#6

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

  Alt 2. Dez 2013, 13: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 13:10 Uhr)
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#7

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

  Alt 2. Dez 2013, 13: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.625 Beiträge
 
Delphi 12 Athens
 
#8

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

  Alt 2. Dez 2013, 13: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
482 Beiträge
 
Delphi XE6 Professional
 
#9

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

  Alt 2. Dez 2013, 13: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.625 Beiträge
 
Delphi 12 Athens
 
#10

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

  Alt 2. Dez 2013, 13: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
Antwort Antwort
Seite 1 von 2  1 2      


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 09:51 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