Autor Beitrag
daywalker0086
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 243


Delphi 2005 Architect
BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 762
Erhaltene Danke: 127



BeitragVerfasst: Di 12.06.18 18:40 
Eventuell so, wobei SZahl schon die als String umgewandelte Zahl ist:

ausblenden 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 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 243


Delphi 2005 Architect
BeitragVerfasst: Do 14.06.18 18:23 
Dankeschön ub60,
läuft :D
GuaAck
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 376
Erhaltene Danke: 32

Windows 8.1
Delphi 10.4 Comm. Edition
BeitragVerfasst: Sa 16.06.18 22:00 
Hallo,

warum nicht einfach SZahl:=inttostr(zahl)?

Habe ich da eine feinheit übersehen?

Gruß GuaAck
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Sa 16.06.18 22:32 
user profile iconGuaAck hat folgendes geschrieben Zum zitierten Posting springen:
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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: 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... :gruebel:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Sa 16.06.18 23:38 
BinToHex ist das richtige, ja.
ub60
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 762
Erhaltene Danke: 127



BeitragVerfasst: So 17.06.18 11:10 
@Narses:

Meintest Du so was:

ausblenden 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 :P .

ub60

Für diesen Beitrag haben gedankt: Narses
GuaAck
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 376
Erhaltene Danke: 32

Windows 8.1
Delphi 10.4 Comm. Edition
BeitragVerfasst: So 17.06.18 11:37 
Hallo alle,

ich habe mal ein kleines Testprogramm gemacht:
ausblenden 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..1OF 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

Dump

(HEX-Dump auch im Anhang)

Demnach gibt doch nur inttostr wie gewünscht die 3331 zurück.

Gruß GuaAck
Einloggen, um Attachments anzusehen!
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 376
Erhaltene Danke: 32

Windows 8.1
Delphi 10.4 Comm. Edition
BeitragVerfasst: 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: 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



BeitragVerfasst: Mo 18.06.18 08:47 
- Nachträglich durch die Entwickler-Ecke gelöscht -
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 18.06.18 12:30 
Moin!

user profile iconFrühlingsrolle hat folgendes geschrieben Zum zitierten Posting springen:
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. :idea: 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



BeitragVerfasst: Di 19.06.18 02:33 
- Nachträglich durch die Entwickler-Ecke gelöscht -