| Autor |
Beitrag |
R4id
      
Beiträge: 28
Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
|
Verfasst: 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:
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..8] of string = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); LUnits: array [0..8] of 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:
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 
      
Beiträge: 28
Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
|
Verfasst: 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
      
Beiträge: 8721
Erhaltene Danke: 191
Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
|
Verfasst: 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 ...
_________________ 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 
      
Beiträge: 28
Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
|
Verfasst: Mo 20.07.09 19:00
Naja...
Delphi
Delphi-Quelltext 1: 2: 3: 4: 5:
| var Bytes: string; begin Bytes := BytesToString(150, true); end; |
Mein C++ Code
C#-Quelltext 1: 2: 3:
| AnsiString Bytes;
Bytes = BytesToString(150, true); |
Dein C++ Code
C#-Quelltext 1: 2: 3:
| AnsiString Bytes;
Bytes = BytesToString(150, true); |
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?
Gruß
[EDIT] Warum soll jaenicke weggucken?? ^^
|
|
BenBE
      
Beiträge: 8721
Erhaltene Danke: 191
Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
|
Verfasst: 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 ...
R4id hat folgendes geschrieben : | | [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 
      
Beiträge: 28
Win XP Home, Win XP Prof.
D7 Prof., D2006 Arch., BCB2006 Arch.
|
Verfasst: 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ß
|
|
|