Autor Beitrag
DiplomatF200
Hält's aus hier
Beiträge: 1



BeitragVerfasst: Do 05.02.04 18:14 
tach,
Folgende Frage: Wie kann ich Dezimalzahlen in Binärzahlen umwandeln. Und wie schaffe ich es, dass ich auch Komma-Dezimalzahlen in Binärzahlen umwandeln kann. Leute helft mir. Danke.
---------------------------------------------------------------


Unglücklich das Land, das keine Helden hat. Nein. Unglücklich das Land, das Helden nötig hat.
ChrHohbein
Hält's aus hier
Beiträge: 4



BeitragVerfasst: Do 05.02.04 18:51 
Hm, ohne groß zu schauen ob es da schon tools gibt würde ich vermuten:

Jede Stelle der Binärzahl einzeln auf 1 oder 0 prüfen. von pos 0 bis ende
und für jeden schritt deinen zähler mal 2 nehmen.
wenn eine 1 vorhanden ist nimmst du dein ergebnisswert + zähler.

für die Kommazahlen fehlt mir leider gerade die berechnungsformel, aber die habe ich hier auch irgendwo rumliegen (war ne aufgabe von Etechnik)
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Do 05.02.04 20:45 
ohne jetzt zu wissen, wie das IEEE FOrmat für Gleitkommazahlen definiert ist, hier eine Brachialmethode.
Einfach einen varianten-Record erstellen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
type
  Test= Record
  case boolean of
  false:  (Zahl: Double);
  true : (Bytes:Array[0..7] of Byte);//Double ist 8 Bytes groß
end;

var
  x: Test;
begin
  X.Zahl:=3.1454323443;
  X.Bytes[0]...
...


sodele, X.Bytes[...] brauchst du nur noch verrödeln in Bits und dann haste dein Ergebnis.

Achte auf little Edian. (das kleinste Byte kommt zuerst)

grez
msch

_________________
ist das politisch, wenn ich linksdrehenden Joghurt haben möchte?
obbschtkuche
Gast
Erhaltene Danke: 1



BeitragVerfasst: Do 05.02.04 20:55 
Hier ein paar fkten:

ausblenden volle Höhe 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:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
type
 // Falscher Name, aber egal ;)
 TIntAr = array[0..255] of byte;
 PIntAr = ^TIntAr;

// byte => bits 
// Wenn fill = true, wird immer auf 8 Zeichen aufgefüllt
function bytetobit(b: byte; fill: boolean = false): string;
begin
 result := '';
 while b > 0 do
 begin
  result := inttostr(b mod 2) + result;
  b := b shr 1;
 end;
 if fill then
  while length(result) < 8 do
   result := '0' + result;
end;

// Word => bits
// Es wird immer auf 16 Zeichen aufgefüllt
function wordtobit(w: word): string;
begin
 result := bytetobit(hibyte(w), true) + bytetobit(lobyte(w), true);
end;

// DWord => bits
// Es wird immer auf 32 Zeichen aufgefüllt
function dwordtobit(dw: dword): string;
begin
 result := wordtobit(hiword(dw)) + wordtobit(loword(dw));
end;

// Einen Byte-Array => bits
// Es wird immer auf length * 8 Zeichen aufgefüllt
function IntArToBit(mr: PIntAr; length: byte): string;
var
 i: byte;
begin
 result := '';
 for i := 0 to length-1 do
  result := result + bytetobit(mr[i], true);
end;

// einen Real in Bits
function RealToBit(var mreal: real): string;
begin
 result := IntArToBit(@mreal, 8); // real hat 8 bits
end;

// 8 bits in einen Byte
procedure BitToByte(s: string; var b: byte);
var
 ab: byte;
 i: byte;
begin
 ab := 128;
 b := 0;
 for i := 1 to 8 do
 begin
  if s[i] = '1' then
   inc(b, ab);
  ab := ab shr 1;
 end;
end;

// 80 bits in einen real
procedure BitToReal(bits: string; var mreal: real);
var
 a: PIntAr;
 i: byte;
begin
 a := @mreal;
 form1.memo1.lines.add(bits);
 for i := 0 to 7 do
 begin
  form1.Memo1.lines.add(copy(bits, i*8+1, 8));
  BitToByte(copy(bits, i*8+1, 8), a^[i]);
 end;
end;
Sven
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 314


D6 Ent, K3 Pro (patched)
BeitragVerfasst: Fr 06.02.04 07:38 
MSCH hat folgendes geschrieben:

...
Achte auf little Edian. (das kleinste Byte kommt zuerst)
...


Erzähl den Leuten nichts falsches. Das gilt nur für das Speichern auf Datenträgern, nicht für Daten im Hauptspeicher :!:

_________________
MDK 9.1, Kernel 2.4.21, KDE 3.1 Kylix 3 Pro (patched), nutze aber auch Windows