Recent

Author Topic: [SOLVED] Ascii code to string  (Read 833 times)

CM630

  • Hero Member
  • *****
  • Posts: 1404
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
[SOLVED] Ascii code to string
« on: May 23, 2025, 08:35:22 am »
Oddly, I cannot find the answer.
I need to convert an ASCII code to a string.
In other words, I need to convert the number $48 to the string 'H'. How?
« Last Edit: May 23, 2025, 08:43:37 am by CM630 »
Лазар 4,0 32 bit (sometimes 64 bit); FPC3,2,2

Fibonacci

  • Hero Member
  • *****
  • Posts: 754
  • Internal Error Hunter
Re: Ascii code to string
« Reply #1 on: May 23, 2025, 08:37:28 am »
https://www.freepascal.org/docs-html/rtl/system/chr.html

or

Code: Pascal  [Select][+][-]
  1. str := #$48;

CM630

  • Hero Member
  • *****
  • Posts: 1404
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: [SOLVED] Ascii code to string
« Reply #2 on: May 23, 2025, 08:44:23 am »
Thanks, this seems to work:

Code: Pascal  [Select][+][-]
  1. String(#$48)

or

Code: Pascal  [Select][+][-]
  1. String(char($48))
Лазар 4,0 32 bit (sometimes 64 bit); FPC3,2,2

Thaddy

  • Hero Member
  • *****
  • Posts: 17178
  • Ceterum censeo Trump esse delendam
Re: [SOLVED] Ascii code to string
« Reply #3 on: May 23, 2025, 12:44:46 pm »
The casts are not necessary. That puts people on the wrong foot imho.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. begin
  3.   writeln(#$48);// this is already a string in hex notation: H.
  4. end.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

CM630

  • Hero Member
  • *****
  • Posts: 1404
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: [SOLVED] Ascii code to string
« Reply #4 on: May 23, 2025, 09:47:01 pm »
Indeed. Both line result in H.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i : byte = $48;
  4. begin
  5.   ShowMessage (String(char(i)));
  6.   ShowMessage (char(i));  
  7. end;
Лазар 4,0 32 bit (sometimes 64 bit); FPC3,2,2

 

TinyPortal © 2005-2018