Autor Beitrag
R4id
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 28

Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
BeitragVerfasst: Sa 18.07.09 23:34 
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:
ausblenden 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:
ausblenden volle Höhe 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 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 28

Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
BeitragVerfasst: Sa 18.07.09 23:49 
Hat sich erledigt, hab was gefunden -.-

ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 19.07.09 02:28 
Ich würd das noch ein wenig anders umsetzen (jaenicke bitte weggucken):

ausblenden 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 ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
R4id Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 28

Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
BeitragVerfasst: Mo 20.07.09 19:00 
Naja...

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


Mein C++ Code
ausblenden C#-Quelltext
1:
2:
3:
AnsiString Bytes;

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


Dein C++ Code
ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 20.07.09 19:33 
Oops, hab was übersehen ...

ausblenden 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 ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
R4id Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 28

Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
BeitragVerfasst: Mo 20.07.09 19:56 
ausblenden 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ß