Autor Beitrag
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: So 30.03.03 15:52 
Hallo,

Ich denke mal, das dürfte reine Mathematik sein, aber so abstrakt kann ich nun wieder doch (noch) nicht denken, oder ich hab' irgendwas vergessen...
Ich muss 32bittige Zahlen in einem anderen binären Format darstellen.
Normale LongWords und LongInts werden im Speicher von "von rechts nach links" geschrieben (niederwertige Stellen also rechts) und linksbündig ausgerichtet. Für jedes Byte stehen dazu 8 bit zur Verfügung.
Die Zahl 400 sieht also folgendermaßen aus: $90 01 00 00 (Hexadezimal, keine normalen Zahlen ;-) )
Dabei sieht das Speicherschema binär so aus:
xxxxxxxx xxxxxxx xxxxxxxx xxxxxxxx
Alle 8 Bits sind durch Zahlen belegbar.
Mein Zielformat ist rechtsbündig ausgerichtet und speichert Zahlen "von links nach rechts" bit 7 Bit pro Byte.
Speicherschema binär:
0xxxxxxx 0xxxxxxx 0xxxxxxx 0xxxxxxx
Das siebte Bit jedes Bytes muss 0 sein, alle anderen sind zur Speicherung verfügbar, dadurch hat die Zahl nur noch 28 bit.
Bei mir sähe 400 also so aus: $00 00 03 10. (hoffentlich richtig)

Das Problem: Die Konvertierung.
Vom 28bit-Format in das normale 32bit-Format habe ich schon eine Funktion geschrieben:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
function Conv28to32(Value: array of Byte): Cardinal;
begin
  result := Value[0] * 2097152 +
            Value[1] * 16384   +
            Value[2] * 128     +
            Value[3];
end;

Wobei Value ein array of Byte ist, daß einen 28bit-Integer enthält, der noch rechtsbündig ausgerichtet ist.

Nur umgekehrt habe ich keine Ahnung, wie ich das anstellen kann. Dort ist es nämlich ein normaler LongWord, der ein array of Byte werden muss (durch typecasting kann auch ein LongWord rauskommen, das wäre kein Problem, nur die Bits müssen stimmen).

Irgendwelche Vorschläge?

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: So 30.03.03 16:39 
Hier mal eine schnellere Variante von deiner Konvertierung.
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
type
  Int28 = Cardinal;
  TInt28 = array[0..3] of Byte;

function Int28ToInt32(const Value: Int28): Cardinal;
begin
  Result := Cardinal(TInt28(Value)[3]) shl  0 or
            Cardinal(TInt28(Value)[2]) shl  7 or
            Cardinal(TInt28(Value)[1]) shl 14 or
            Cardinal(TInt28(Value)[0]) shl 21;
end;

function Int32ToInt28(Value: Cardinal): Int28;
begin
   TInt28(Result)[3] := ((Value shr  0) and $7f);
   TInt28(Result)[2] := ((Value shr  7) and $7f);
   TInt28(Result)[1] := ((Value shr 14) and $7f);
   TInt28(Result)[0] := ((Value shr 21) and $7f);
end;

_________________
Ist Zeit wirklich Geld?
tommie-lie Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: So 30.03.03 18:18 
Danke!
Daran habe ich nicht gedacht... Ich war ständig auf der Suche nach einem array-losen Weg, aber so ist es auch ideal.

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert