-4

There is this code block I used for a while.

Definitions:

type TItem = record
 Code          : string[255];
 Description   : string[255];
 Warning       : string[255];
 Fold          : integer;     //position where clamp to fold, 0 = flat, about 170 when needs to be folded
 PrintStrength : integer;     //put 0 if default to machine parameters, normally about 56
     PrintSpeed    : integer;     //put 0 if default to machine parameters, normally about 130
end;
var
  Item : TItem;
  LabelBitmap : TBitmap;

Code Block:

procedure DosyaYazdir(filename : string; Bitmap : Tbitmap);
var FileStream : TFileStream;
    siz: int64;
begin

  try
    FileStream := TFileStream.Create(filename, fmCreate);
    siz := SizeOf(Item);
    FileStream.Write(siz,SizeOf(siz));
    FileStream.Write(Item,siz);
    //Bitmap.SaveToStream(FileStream);
    form1.image2.picture.bitmap.SaveToStream(filestream);
    form1.Memo1.Lines.SaveToStream(FileStream,TEncoding.Unicode); 
    FileStream.free;
  except
    Application.MessageBox('Wrong file name','Kayıt Hatası :',MB_ICONERROR);
  end;
  form1.memo1.lines.clear;
end;

I call like this: (I don't do anything with LabelBitmap)

dosya:='C:\Custom_Etiket.itm';
DosyaYazdir(dosya,LabelBitmap);

Which saves the given strings (form1.memo1.Lines) into a file with given name. This was readymade I found on internet and now unfortunately I want to change my software to C#.

Since this was readymade, I don't really know what this code does. Because when I try to do in C#, I can not get the same output (with same encoding or whatsoever)

Here is the sample screenshot from the file it generates: (You can see the text between NULs but it has some kind of encoding) delphi generated file screenshot part

What does this Delphi Code block do? Or what should I do to get the same encoding?

//PS. Feel free to edit the title if it doesn't describe the situation well enough.

Community
  • 1
  • 1
Mustafa
  • 825
  • 3
  • 14
  • 37
  • did you do a Google Search on `Encoding or `Delphin TEncoding.Unicode` this will explain that it's the following encoding below `encoding to UTF8` – MethodMan Mar 25 '15 at 19:41
  • @Method No, it's UTF-16 – David Heffernan Mar 25 '15 at 19:50
  • @MethodMan Yes, of course I did many google searches. I saved the Text in Unicode encoding & UTF8 in C#. I didn't get the same result. – Mustafa Mar 25 '15 at 19:52
  • have you looked at this http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/SysUtils_TEncoding.html it gives examples of the Encoding types for the TEncoding Class.. perhaps you can mess around with the different types listed on this link.. – MethodMan Mar 25 '15 at 19:55
  • I will give a shot to UTF-16LE as @DavidHeffernan suggested. – Mustafa Mar 25 '15 at 19:58
  • Also note that string[255] is ShortString, short AnsiString. For compatibility you should probably use "packed record". But if you don't understand what you do, then it will be difficult for you to port the code. – smooty86 Mar 25 '15 at 20:37
  • @smooty86 Why would packing improve compatibility? If the record is aligned, won't packing it change its layout? Why are Delphi devs obsessed with packing records? In this case the layout is the same packed and aligned, so the matter is moot. – David Heffernan Mar 25 '15 at 20:40
  • I don't work with that so yes, it is just obsession from past. – smooty86 Mar 25 '15 at 20:53

1 Answers1

0

What does this Delphi Code block do?

The Delphi code does the following:

  • Write a 64 bit integer to the file.
  • Write the binary representation of Item to the file. We don't know what Item is.
  • Write a bitmap to the file in bmp format.
  • Write a text file to the file using UTF-16LE encoding.

This is a really poorly designed file. You really should be using JSON or XML or YAML or indeed any structured format for this data.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490