Entwickler-Ecke

Sonstiges (Delphi) - Delphi -> C++ Übersetzung...?


R4id - Sa 18.07.09 23:34
Titel: Delphi -> C++ Übersetzung...?
Hallo alle zusammen... Ich weiss leider nicht wo ich das sonst reinsetzen soll deshalb wird erstmal hier geparkt ^^
Ich würde gerne folgenden Code von Delphi in C++ umsetzen:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
uses
  Math;

[...]

function CalculateBytes(const Value: Int64; const Short: Boolean): string;
const
  SUnits: array [0..8of string = ('B''KB''MB''GB''TB''PB''EB''ZB''YB');
  LUnits: array [0..8of string = ('Byte''KiloByte''MegaByte''GigaByte''TeraByte''PetaByte''ExaByte''ZettaByte''YottaByte');
var
  X: string;
  I: Integer;
begin
  for I := 1 to Length(SUnits) - 1 do
    if Power(2, I * 10) > Value then
      Break;

  if Short then
    X := SUnits[I - 1]
  else
    X := LUnits[I - 1];

  Result := FloatToStr(RoundTo(Value / Power(2, (I - 1) * 10), - 2)) + ' ' + X;
end;


Bisher hab ich folgendes:

Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
#include <math.h>

[...]

AnsiString BytesToString(double Value, bool Short)
{
  const AnsiString SUnits[9] = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
  const AnsiString LUnits[9] = {"Byte", "KiloByte", "MegaByte", "GigaByte", "TeraByte", "PetaByte", "ExaByte", "ZettaByte", "YottaByte"};

  AnsiString S, TEMP;
  int I;

  for (I = 1; I <= 9 - 1; I++)
  {
    if (pow(2, I * 10) > Value)
    {
      break;
    }
  }

  if (Short)
  {
    S = SUnits[I - 1];
  }
  else
  {
    S = LUnits[I - 1];
  }

  return FloatToStr(RoundTo(Value / pow(2, (I - 1) * 10), -2)) + S;
}


Allerdings will das ganze nicht wirklich weil ich keine "RoundTo"-Funktion finde die der von Delphi entspricht.

Gruß


R4id - Sa 18.07.09 23:49

Hat sich erledigt, hab was gefunden -.-


Quelltext
1:
2:
3:
4:
5:
double RoundTo(double number, int digits)
{
  double v[] = {1, 10, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8};
  return floor(number * v[digits] + 0.5) / v[digits];
}


Gruß


BenBE - So 19.07.09 02:28

Ich würd das noch ein wenig anders umsetzen (jaenicke bitte weggucken):


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
#include <math.h>

[...]

AnsiString BytesToString(double Value, bool Short)
{
  const AnsiString Units[2][9] = {
    {"B""KB""MB""GB""TB""PB""EB""ZB""YB"},
    {"Byte""KiloByte""MegaByte""GigaByte""TeraByte""PetaByte""ExaByte""ZettaByte""YottaByte"}
    };

  AnsiString S;
  int I;

  for (I = 1; (I < 9) && (Value > (1024ULL << I)); I++);

  S = Units[Short][I - 1];

  return FloatToStr(RoundTo(Value / (1024ULL << (I - 1)), -2)) + S;
}


Hab grad keinen C++-Compiler zur Hand, sollte aber so funktionieren ...


R4id - Mo 20.07.09 19:00

Naja...

Delphi

Delphi-Quelltext
1:
2:
3:
4:
5:
var
  Bytes: string;
begin
  Bytes := BytesToString(150, true); // Bytes = "150B"
end;


Mein C++ Code

C#-Quelltext
1:
2:
3:
AnsiString Bytes;

Bytes = BytesToString(150true); // Bytes = "150B"


Dein C++ Code

C#-Quelltext
1:
2:
3:
AnsiString Bytes;

Bytes = BytesToString(150true); // Bytes = "0,15B"


Also da kommen jetzt 2 verschiedene ergebnisse raus, einmal 150B wo mit dem Original Delphi Code übereinstimmt, und einmal 0,15B.

Ist jetzt mein Originaler Delphi und C++ Code falsch oder deiner? :gruebel:

Gruß

[EDIT] Warum soll jaenicke weggucken?? ^^


BenBE - Mo 20.07.09 19:33

Oops, hab was übersehen ...


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
#include <math.h>

[...]

AnsiString BytesToString(double Value, bool Short)
{
  const AnsiString Units[2][9] = {
    {"B""KB""MB""GB""TB""PB""EB""ZB""YB"},
    {"Byte""KiloByte""MegaByte""GigaByte""TeraByte""PetaByte""ExaByte""ZettaByte""YottaByte"}
    };

  AnsiString S;
  int I;

  for (I = 1; (I < 9) && (Value > (1024ULL << I)); I++);

  S = Units[Short][--I];

  return FloatToStr(RoundTo(Value / (1ULL << (I*10)), -2)) + S;
}


So sollte es schon eher funzen ...

user profile iconR4id hat folgendes geschrieben Zum zitierten Posting springen:
[EDIT] Warum soll jaenicke weggucken?? ^^

Hängt mit einer Diskussion neulich hier im Forum zusammen ...


R4id - Mo 20.07.09 19:56


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
AnsiString BytesToString(double Value, bool Short)
{
  const AnsiString Units[2][9] = {
  {"B""KB""MB""GB""TB""PB""EB""ZB""YB"},
  {"Byte""KiloByte""MegaByte""GigaByte""TeraByte""PetaByte""ExaByte""ZettaByte""YottaByte"}
  };

  AnsiString S;
  int I;

  for (I = 1; (I < 9) && (Value > (1024ULL << I)); I++);

  S = Units[Short][--I];

  return FloatToStr(RoundTo(Value / (1ULL << (I*10)), -2)) + S;
}


Die 2 arrays vertauschen und das Minus vor der 2 weg (Der Minus fehler hatte ich auch, wenn da ein Minus ist kommt immer 0 raus) dann funktioniert es perfekt!

Danke...!

Gruß