Search code examples
c#delphidelphi-7

migrate delphi code to c#, char code number


i need to migrate some delphi code to c# (.net) for my mv4 apliccation. which will replace some functionalities of the existing delphi aplication, but i need to use some specific function.

the main problem is when i try to get a char from a string like:

FText = "123456";
i = 1;

delphi:

a := Integer(FText[i]);

c#:

a = (int)FText[i];

but c# returns 50 and delphi 49


Solution

  • Delphi has historically used "Length-prefixed" strings where the length indicator was a string[0]. Placing the first character of the string at index 1. Since the introduction of "long strings" in Delphi, the byte count is no longer at index 0, but the strings continue to use 1-based indexing.

    C# uses zero-based indexing for strings. When you convert any string code from Delphi to C#, you will need to deal with the different indexing scheme.