Wednesday, March 14, 2007

Problem

You need to find the numeric code for a character (either ASCII or Unicode).

Solution

namespace Cookbook1_2;

interface

type
  ConsoleApp = class
  public
    class method Main;
  end;

implementation

class method ConsoleApp.Main;
begin
  var ch      : char    := 'A';
  var chAsInt : Integer := Integer(ch);
  Console.WriteLine( chAsInt );
end;

end.

The ASCII or Unicode value of character is obtained by casting the character to an Integer. Also note that I have forgo Chromes type inference and explicitly declared the type for each variable.