0

this is a first-time question from a beginner who's learning c#. I had just learned about how you can "convert numbers to characters" by explicitly typecasting an integer into the char method, therefore being able to print the character it shares binary with (sorry about my sucky terminology). But when I try to do this, all the terminal prints is a question mark. Here's the code: char a = (char)977;

Console.WriteLine(a); When I run this, all I get is ? Thanks for taking the time to help out a beginner.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
Tom Catt
  • 13
  • 2

1 Answers1

0

You're trying to print a character in your terminal or console, but the font that your console is using might not have a glyph for that character, or it might not be configured with the correct character encoding.

The character in question is the Greek theta symbol (ϑ), unicode character U+03D1 (note here how 03D1 is the hexadecimal representation of decimal 977).

Running the code in an environment with a font that does have this character, will successfully print it. (I tried on https://dotnetfiddle.net/ and got the character to print, but even there it will depend on the fonts installed on your system.)

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • 1
    This is incorrect. It's not because the glyph isn't there, it's because the encoding for the console defaults to ASCII, and needs to be set to UTF8 to allow the non-ASCII character to be displayed. – Peter Duniho Jun 03 '20 at 07:03
  • @PeterDuniho When it comes to that, your guess is as good as mine. We have no information about what console/terminal OP is using. Don't even know which operating system this is running on. But I'll update my answer to add the other possibility as well. – Robby Cornelissen Jun 03 '20 at 07:04
  • 1
    If you don't have enough information to answer the question, you shouldn't be posting an answer. That said, the fact remains that question marks are printed when the encoding doesn't match, while missing glyphs are represented in the console window with a special "question mark in a box" character (using Consolas...different fonts use different glyphs, but never a plain question mark). The OP says a question mark is printed, not some missing-glyph character. – Peter Duniho Jun 03 '20 at 07:08
  • @PeterDuniho The way unrecognized bytes or byte sequences are represented in a given terminal is entirely up to the terminal. Are you speaking for all terminal applications out there? And if the terminal is configured to represent ASCII, why is the `D1` byte not simply represented as `ü`? Or you meant 7-bit ASCII and not extended ASCII? – Robby Cornelissen Jun 03 '20 at 07:16
  • 1
    I'll try all your solutions out, thanks, this helped. – Tom Catt Jun 03 '20 at 14:35
  • _"if the terminal is configured to represent ASCII, why is the D1 byte not simply represented as ü?"_ -- it's not the "terminal" that is configured that way, it's the `Console` class, a .NET construct. It's .NET handling the encoding and replacing the unknown character with `'?'`. That's the default behavior for fallback for the `Encoding` implementations. That's how we know it's not the terminal changing the character, but rather .NET. ... – Peter Duniho Jun 03 '20 at 15:18
  • ... In any case, you accusing me that _I_ don't know the precise scenario here, when it's you who posted the answer prematurely, is pretty funny. I don't have any obligation to defend my knowledge of the author's scenario; you do. – Peter Duniho Jun 03 '20 at 15:18