Recent

Author Topic: how to convert old strings in unicode  (Read 1063 times)

HobbyDev

  • New Member
  • *
  • Posts: 29
how to convert old strings in unicode
« on: January 08, 2025, 06:45:41 pm »
Hi there,
its almost 10 years ago I stopped programming in Delphi and now I want to start in Lazarus again. Back in these days I wrote an application to store the information of my Mini-DV video tapes. The video directory contains a list of tapes where each tape has a list of titles which holds all scenes in a list belonging to that title. Every scenes is stored as an MKV file. Because I am using digiKam now to archive all information I don't need my old application any more. But I want to keep the information. So I thought to store the MKV's in the file structure where e.g. the title is a folder. And I want to store the scene's description inside the MKV using ffmpeg. And here is the point where I got lost: the conversion from old string into the new unicode world - at least for me it is new :-). When I do read the old strings they have the "?" sign where ever I used one of the German special characters like ÄäÖöÜüß and I don't know how to convert it so I can use it as e.g. a folder name. This is the code I used to store the title information:
Code: Pascal  [Select][+][-]
  1. procedure TTitel.Store(var FS : TFileStream);
  2. var AnzahlItems : Integer;
  3.     i, l        : Integer;
  4.     ASzene      : TSzene;
  5. begin
  6.   AnzahlItems := pred(Szenenliste.Count);
  7.   l := Length(Titel);
  8.   FS.Write(l, SizeOf(l));
  9.   FS.Write(Titel[1], l);
  10.   FS.Write(AnzahlItems, SizeOf(AnzahlItems));
  11.   for I := 0 to AnzahlItems do
  12.     begin
  13.       ASzene := TSzene(Szenenliste.Items[i]);
  14.       ASzene.Store(FS);
  15.     end;
  16. end;

where Titel is defined in the TTitle class as a string.
Reading a titel is done with this method:
Code: Pascal  [Select][+][-]
  1. Constructor TTitel.CreateFromStream(var FS : TFileStream; AParent : TTape);
  2. var AnzahlItems : Integer;
  3.     i,l         : Integer;
  4.     ASzene      : TSzene;
  5. begin
  6.   inherited Create;
  7.   Parent      := AParent;
  8.   Szenenliste := TList.Create;
  9.   FS.Read(l, sizeOf(l));
  10.   SetLength(Titel, l);          // then the string itself
  11.   FS.Read(Titel[1], l);
  12.   FS.Read(AnzahlItems, sizeOf(AnzahlItems));
  13.   for i := 0 to AnzahlItems do
  14.     begin
  15.       ASzene := TSzene.CreateFromStream(FS, Self);
  16.       Szenenliste.Add(ASzene);
  17.     end;
  18.   if TCF then Parent.TCF := True;
  19. end;
  20.  
To visualize the titles I use a treeview component and add nodes with the title string. And these nodes contain the "?" signs I want to get rid of, but don't know how. I am pretty sure you know it right away  ;D

Many thx. in advanced!

Thaddy

  • Hero Member
  • *****
  • Posts: 16399
  • Censorship about opinions does not belong here.
Re: how to convert old strings in unicode
« Reply #1 on: January 08, 2025, 07:00:56 pm »
You can simply use implicate assignment.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. var
  3.   a:AnsiString = 'test';
  4.   b:Unicodestring;
  5. begin
  6.   b:=a;
  7. end.
  8.  
Don't look for complications where there aren't any.
« Last Edit: January 08, 2025, 07:02:58 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

Zoran

  • Hero Member
  • *****
  • Posts: 1892
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: how to convert old strings in unicode
« Reply #2 on: January 08, 2025, 07:05:38 pm »
Your strings are most probably encoded with win1252 codepage. So try using CP1252ToUTF8 from LConvEncoding unit:
Code: Pascal  [Select][+][-]
  1. uses
  2.   ..., LConvEncoding, ...
  3.  
  4. ...
  5.  
  6. begin
  7.   inherited Create;
  8.   Parent      := AParent;
  9.   Szenenliste := TList.Create;
  10.   FS.Read(l, sizeOf(l));
  11.   SetLength(Titel, l);          // then the string itself
  12.   FS.Read(Titel[1], l);
  13.  
  14.   Titel := CP1252ToUTF8(Titel); // insert this line and try
  15.  
  16.   FS.Read(AnzahlItems, sizeOf(AnzahlItems));
  17.   for i := 0 to AnzahlItems do
  18.     begin
  19.       ASzene := TSzene.CreateFromStream(FS, Self);
  20.       Szenenliste.Add(ASzene);
  21.     end;
  22.   if TCF then Parent.TCF := True;
  23. end;
« Last Edit: January 08, 2025, 07:07:11 pm by Zoran »

ASerge

  • Hero Member
  • *****
  • Posts: 2354
Re: how to convert old strings in unicode
« Reply #3 on: January 08, 2025, 07:13:30 pm »
Code: Pascal  [Select][+][-]
  1. ...
  2.   l := Length(Titel);
  3.   FS.Write(l, SizeOf(l));
  4.   FS.Write(Titel[1], l);
  5. ...
  6.   FS.Read(l, sizeOf(l));
  7.   SetLength(Titel, l);          // then the string itself
  8.   FS.Read(Titel[1], l);
  9.  
In FPC, TStream has WriteAnsiString and ReadAnsiString methods with the same behavior as your code.

HobbyDev

  • New Member
  • *
  • Posts: 29
[solved] Re: how to convert old strings in unicode
« Reply #4 on: January 08, 2025, 09:30:48 pm »
How cool is that!  :)
I checked your idea - Zoran! Perfectly! I am sure the others will work as well but this solution looked that easy I checked it first
Thank you very much. I should have done this earlier and save a lot of time

 

TinyPortal © 2005-2018