Search code examples
c#delphidelphi-7

C# vs Delphi- both codes are giving different value for ‰


Can anyone tell me that why am I getting different values for ‰ symbol? My Delphi7 code is-

k := ord('‰'); //k is of type longint; receiving k as 137

My C# code is-

k = (long)('‰');//k is of type long; receiving k as 8240

Please give me a solution, that how can I get the same value in C#? Because Delphi code solves my purpose.


Solution

  • C# encodes text as UTF-16. Delphi 7 encodes text as ANSI. Therein lies the difference. You are not comparing like with like.

    You ask how to get the ANSI ordinal value for that character. Use new Encoding(codepage) to get an Encoding instance that matches your Delphi ANSI encoding. You'll need to know which code page you are using in order to do that. Then call GetBytes on that encoding instance.

    For instance if your code page is 1252 then you'd write:

    enc = new Encoding(1252);
    byte[] bytes = enc.GetBytes("‰");
    

    Or if you want to use the default system ANSI encoding you would use

    byte[] bytes = Encoding.Default.GetBytes("‰");
    

    One wonders why you are asking this question. Have answered many questions here, I wonder if you are performing some sort of encryption, hashing or encoding, and are using as your reference some Delphi code that uses AnsiString variables as byte arrays. In which case, whilst Encoding.GetBytes is what you asked for, it's not what you need. What you need is to stop using strings to hold binary data. Use byte arrays. In both Delphi and C#.