2

I am creating a character to Alt Code Converter. I need to know how to get an Alt Code from a char.

EDIT: In case no one knows what an Alt Code is, it is Alt+3+6 = $. Try it in the comments. If you use a Mac, you can't do it like me.

EDIT 2: Converting the ASCII character to an integer DOES NOT WORK!

2 Answers2

3

For any GUI app in Windows, the code you type with the numeric pad is interpreted according to the machine's default code page. In Western Europe and the Americas, and probably close to your home with a name like yours, it will be Windows-1252. But not in, say, Russia, Eastern Europe, Middle East, etc.

Use Encoding.Default.GetBytes() to avoid taking a dependency on the default code page. If the byte[] you get contains 0x3f ('?') or more than one element then it cannot be entered.

Do beware the trouble with console mode apps. They typically run with a legacy MS-Dos code page encoding, 437 in your neck of the woods but again something very different in another part of the world. The Alt-code now produces a very different character, determined by Console.OutputEncoding. How you untangle this is not obvious from the question, I suppose you'll need to know what specific process is going to consume the code. If it starts to feel like this project was a mistake then you're heading the right way.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    I think the project was a mistake. Honestly, I was bored and started this earlier for fun, but it's started to just become a project which makes me want to rip out my hair! –  Dec 24 '15 at 13:08
-1

The alt code is just the decimal number of a char so you basically just need to convert a char into a number. You could convert it into an int using a conversion method but then you are having to call a method. You can actually just simply cast it as a decimal and it will know what to do.

    static void Main(string[] args)
    {
        char input = '$';

        decimal output = input;

        Console.WriteLine(output);
    }
Keithin8a
  • 961
  • 7
  • 32
  • 1
    @David are you sure? This will give you the Unicode code point. For example the character `È` has code 200. However, Alt-codes have various categories. To type an `È`, you can use `Alt+0200` but you can't type it using three-digit Alt codes. – CodeCaster Dec 24 '15 at 12:36
  • @CodeCaster Just realized that, put in È, gave me 200 which is ╚. That gave me 9562 etc... –  Dec 24 '15 at 12:38
  • @DavidWheatley It all depends on what your scope is really. if you need the extended ascii characters then you will probably need something a bit more clever but if you are only wanting to deal with the basic 127 ascii chart then is there any point in over complicating it? – Keithin8a Dec 24 '15 at 12:52