Recent

Author Topic: [SOLVED] convert all char(Unicode) to integer(or HEX) and inverse??  (Read 29995 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 16659
  • Kallstadt seems a good place to evict Trump to.
Re: [SOLVED] convert all char(Unicode) to integer(or HEX) and inverse??
« Reply #45 on: May 17, 2024, 04:56:22 pm »
This is an old thread, but here is a set of operators for converting string types to TBytes and vice versa;
Code: Pascal  [Select][+][-]
  1.    operator := (const a:ShortString):TBytes;inline;
  2.    begin
  3.      setlength(Result,length(a));
  4.      move(a[1],Result[0],length(a));
  5.    end;
  6.  
  7.    operator := (const a:AnsiString):TBytes;inline;
  8.    begin
  9.      setlength(Result,length(a));
  10.      move(a[1],Result[0],length(a));
  11.    end;
  12.  
  13.    operator := (const a:UTF8String):TBytes;inline;
  14.    begin
  15.      setlength(Result,length(a));
  16.      move(a[1],Result[0],length(a));
  17.    end;
  18.    
  19.    operator := (const a:UnicodeString):TBytes;inline;
  20.    begin
  21.      setlength(Result,length(a));
  22.      move(a[1],Result[0],length(a));
  23.    end;
  24.  
  25.    operator := (const a:TBytes):shortstring;inline;
  26.    begin
  27.      SetString(Result,PAnsiChar(a),length(a));
  28.    end;
  29.  
  30.    operator := (const a:TBytes):AnsiString;inline;
  31.    begin
  32.      SetString(Result,PAnsiChar(a),length(a));
  33.    end;
  34.  
  35.    operator := (const a:TBytes):UTF8String;inline;
  36.    begin
  37.      SetString(Result,PAnsiChar(a),length(a));
  38.    end;
  39.  {// currently has problems. When fixed, uncomment.
  40.    operator := (const a:TBytes):UnicodeString;inline;
  41.    begin
  42.      SetString(Result,PAnsiChar(a),length(a));
  43.    end;
  44. }
  45.  
It's just to remember forever they are there.
length() returns the length in bytes for all string types.
« Last Edit: May 18, 2024, 10:44:25 am by Thaddy »
But I am sure they don't want the Trumps back...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5906
  • Compiler Developer
Re: [SOLVED] convert all char(Unicode) to integer(or HEX) and inverse??
« Reply #46 on: May 22, 2024, 09:56:21 pm »
length() returns the length in bytes for all string types.

No, it does not. ::) For UnicodeString the length is in WideChar elements, not in Bytes.

Code: Pascal  [Select][+][-]
  1. program tstr;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. var
  6.   us: UnicodeString;
  7.  
  8. begin
  9.   us := 'Hello World';
  10.   Writeln(Length(us), ' ', SizeOf(us[1])); // will print "11 2"
  11. end.

 

TinyPortal © 2005-2018