Autor Beitrag
Schamese
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17

Windows 2008 Server
Delphi 5 Ent., C# (Visual Studio 2008)
BeitragVerfasst: Fr 20.06.08 21:15 
Hi, Community!

Ich habe eine Funktion geschreiben, mit der ich aus einem Int64 ein bestimmtes Byte herauskriegen möchte.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
function GetByte(value: Int64; byteNumber: Byte): Byte;
begin
  Write(IntToHex(value, 16) + '[' + IntToStr(byteNumber) + '] --> ');
  Result := (value shl (byteNumber * 8)) and $FF00000000000000;
  WriteLn(IntToHex(Result, 2));
end;


Aber sie funktioniert nicht.

Die Ausgabe für z.B.
ausblenden Delphi-Quelltext
1:
2:
for i := 0 to 7 do
  bytes[i] := GetByte($A, i);
ist immer:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
000000000000000A[0] --> 00
000000000000000A[1] --> 00
000000000000000A[2] --> 00
000000000000000A[3] --> 00
000000000000000A[4] --> 00
000000000000000A[5] --> 00
000000000000000A[6] --> 00
000000000000000A[7] --> 00


Soll wäre ja:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
000000000000000A[0] --> 00
000000000000000A[1] --> 00
000000000000000A[2] --> 00
000000000000000A[3] --> 00
000000000000000A[4] --> 00
000000000000000A[5] --> 00
000000000000000A[6] --> 00
000000000000000A[7] --> 0A


Aber warum?

Gruß, Christian.


Moderiert von user profile iconNarses: Topic aus Windows API verschoben am So 22.06.2008 um 02:31
Sirke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 208
Erhaltene Danke: 2



BeitragVerfasst: Fr 20.06.08 21:19 
Die Konstruktion finde ich sehr komisch, weil deine Maskierung kein Byte als Resultat hat! Versuchts mal mit:
ausblenden Delphi-Quelltext
1:
Result := (value shr (8 * byteNumber)) and $FF;					
Schamese Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17

Windows 2008 Server
Delphi 5 Ent., C# (Visual Studio 2008)
BeitragVerfasst: Fr 20.06.08 21:21 
Shon besser. Aber jetzt kommt die Ausgabe immer 2 mal:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
000000000000000A[0] --> 0A
000000000000000A[1] --> 00
000000000000000A[2] --> 00
000000000000000A[3] --> 00
000000000000000A[4] --> 0A
000000000000000A[5] --> 00
000000000000000A[6] --> 00
000000000000000A[7] --> 00
Sirke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 208
Erhaltene Danke: 2



BeitragVerfasst: Fr 20.06.08 22:22 
Sehr komisch :S ... bei mir ist das nämlich nicht der Fall? Versuche mal eine ander Zahl als Eingabe, z.B. $0123456789ABCDEF.
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: Sa 21.06.08 00:01 
SHL und SHR sind in älteren Delphi-Versionen nur für 32-Bit-Zugriffe implementiert; wenn man 64 Bit haben will, muss man das über einen PByte-Zeiger auf die Variable machen, den man mit wetwas Pointer-Arithmetik manipuliert ...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
function GetByte(value: Int64; byteNumber: Byte): Byte;
begin
  Write(IntToHex(value, 16) + '[' + IntToStr(byteNumber) + '] --> ');
  Result := PByte(Integer(PByte(@value)) + byteNumber)^;
  WriteLn(IntToHex(Result, 2));
end;

_________________
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.
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Sa 21.06.08 00:29 
user profile iconBenBE hat folgendes geschrieben:
SHL und SHR sind in älteren Delphi-Versionen nur für 32-Bit-Zugriffe implementiert

Hast du zufällig nähere Infos wie alt? Das würde glaub ich ein Problem erklären was ich neulich hatte...

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
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: Sa 21.06.08 12:06 
user profile iconMartok hat folgendes geschrieben:
user profile iconBenBE hat folgendes geschrieben:
SHL und SHR sind in älteren Delphi-Versionen nur für 32-Bit-Zugriffe implementiert

Hast du zufällig nähere Infos wie alt? Das würde glaub ich ein Problem erklären was ich neulich hatte...

Also zumindest für D7 sind's noch 32 Bit, für neuere Versionen hab ich's noch nicht getestet.

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