Autor |
Beitrag |
daywalker0086
Beiträge: 243
Delphi 2005 Architect
|
Verfasst: Di 12.06.18 17:39
Hallo Freunde des Orakels,
ich wollte mal fragen wie ich am besten eine Zahl in ihre Ascii Repräsentation umwandle.
Folgendes soll erreicht werden:
zahl: integer;
ascii: string;
ascii := zahl -->
das heißt ich habe als Zahl 31.
Jetzt soll dies umgewandelt werden zu ascii 3331
oder 1 zu 31
Die Zahlen werden nicht größer als zweistellig als 99 zu 3939
Hat da jemand eine schnelle Lösung?
Grüße Christian
|
|
ub60
Beiträge: 762
Erhaltene Danke: 127
|
Verfasst: Di 12.06.18 18:40
Eventuell so, wobei SZahl schon die als String umgewandelte Zahl ist:
Delphi-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9:
| var SZahl, Ascii: String; i: Integer; begin SZahl:=EditZahl.Text; Ascii:=''; for i:=1 to Length(SZahl) do Ascii:=Ascii+IntToHex(Ord(SZahl[i]), 2); EditAscii.Text:=Ascii; end; |
ub60
|
|
daywalker0086
Beiträge: 243
Delphi 2005 Architect
|
Verfasst: Do 14.06.18 18:23
Dankeschön ub60,
läuft
|
|
GuaAck
Beiträge: 378
Erhaltene Danke: 32
Windows 8.1
Delphi 10.4 Comm. Edition
|
Verfasst: Sa 16.06.18 22:00
Hallo,
warum nicht einfach SZahl:=inttostr(zahl)?
Habe ich da eine feinheit übersehen?
Gruß GuaAck
|
|
jaenicke
Beiträge: 19285
Erhaltene Danke: 1743
W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
|
Verfasst: Sa 16.06.18 22:32
GuaAck hat folgendes geschrieben : | warum nicht einfach SZahl:=inttostr(zahl)?
Habe ich da eine feinheit übersehen? |
Ja.
Sieh dir mal das Beispiel an. Aus der Zahl 31 soll 3331 werden, sprich Asciizeichen 3 entspricht hexadezimal 33, Asciizeichen 1 wiederum 31. Daher kommt dann 3331 heraus.
|
|
Narses
Beiträge: 10182
Erhaltene Danke: 1255
W10ent
TP3 .. D7pro .. D10.2CE
|
Verfasst: Sa 16.06.18 23:07
Moin!
Ich verstehe den Aufwand nicht, reden wir nicht von BinToHex() und HexToBin()? Hab grad kein Delphi zur Hand, aber ich könnte schwören sowas schonmal benutzt zu haben...
cu
Narses
_________________ There are 10 types of people - those who understand binary and those who don´t.
|
|
jaenicke
Beiträge: 19285
Erhaltene Danke: 1743
W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
|
Verfasst: Sa 16.06.18 23:38
BinToHex ist das richtige, ja.
|
|
ub60
Beiträge: 762
Erhaltene Danke: 127
|
Verfasst: So 17.06.18 11:10
@Narses:
Meintest Du so was:
Delphi-Quelltext 1: 2: 3: 4: 5: 6: 7:
| var SZahl, Ascii: String; begin SZahl:=EditZahl.Text; SetLength(Ascii, 2*Length(SZahl)); BinToHex(PChar(SZahl), PChar(Ascii), Length(SZahl)); EditAscii.Text:=Ascii; end; |
Bin mir gerade bei der Syntax nicht sicher, aber es funktioniert .
ub60
Für diesen Beitrag haben gedankt: Narses
|
|
GuaAck
Beiträge: 378
Erhaltene Danke: 32
Windows 8.1
Delphi 10.4 Comm. Edition
|
Verfasst: So 17.06.18 11:37
Hallo alle,
ich habe mal ein kleines Testprogramm gemacht:
Delphi-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
| PROCEDURE TForm1.Button1Click(Sender: TObject); VAR f: textfile; s: STRING; i: integer; p: ARRAY[0..1] OF char; BEGIN i := 31; assignfile(f, 'Test.txt'); rewrite(f); s := inttostr(i); writeln(f, 'inttostr:', s); s := inttohex(i, 2); writeln(f, 'inttohex:', s); bintohex(pchar(@i), pchar(@p), 1); writeln(f, 'bintohex:', p); closefile(f); END; |
Dann sehe ich mir die Datei "Test.txt" mit einem HEX-Viewer an und sehe:
inttostr:31
inttohex:1F
bintohex:1F
(HEX-Dump auch im Anhang)
Demnach gibt doch nur inttostr wie gewünscht die 3331 zurück.
Gruß GuaAck
Einloggen, um Attachments anzusehen!
|
|
jaenicke
Beiträge: 19285
Erhaltene Danke: 1743
W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
|
Verfasst: So 17.06.18 16:30
Es ging um einen String mit dem Inhalt 31. Sprich deine Variable s. Schreibst du die mit BinToHex weg, kommt das richtige heraus.
|
|
GuaAck
Beiträge: 378
Erhaltene Danke: 32
Windows 8.1
Delphi 10.4 Comm. Edition
|
Verfasst: So 17.06.18 23:14
Sorry Jaenicke,
in der Ausgangsfrage stand:
Zitat: |
zahl: integer;
ascii: string;
ascii := zahl -->
das heißt ich habe als Zahl 31.
Jetzt soll dies umgewandelt werden zu ascii 3331
|
Die 31 ist als Integer gemeint, nicht als String. Ich bleibe bei meinem Votum für inttostr.
Gruß
GuaAck
|
|
Narses
Beiträge: 10182
Erhaltene Danke: 1255
W10ent
TP3 .. D7pro .. D10.2CE
|
Verfasst: So 17.06.18 23:20
Moin!
Aber in dem anderen Thread des TE geht´s genau darum: einen binären Inhalt hexstring-encoded zu senden, also macht es durchaus Sinn.
cu
Narses
_________________ There are 10 types of people - those who understand binary and those who don´t.
|
|
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1
|
Verfasst: Mo 18.06.18 08:47
- Nachträglich durch die Entwickler-Ecke gelöscht -
|
|
Narses
Beiträge: 10182
Erhaltene Danke: 1255
W10ent
TP3 .. D7pro .. D10.2CE
|
Verfasst: Mo 18.06.18 12:30
Moin!
Frühlingsrolle hat folgendes geschrieben : | Das sollte beide Parteien glücklich stimmen: |
Wenn schon zur Wiederverwendung formalisiert, dann solltest du unbedingt auf die generischen Typen verzichten und z.B. AnsiString erzwingen. Das knallt sonst in Unicode-Delphi-Versionen.
cu
Narses
_________________ There are 10 types of people - those who understand binary and those who don´t.
|
|
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1
|
Verfasst: Di 19.06.18 02:33
- Nachträglich durch die Entwickler-Ecke gelöscht -
|
|