How do I use zodiac signs in C# Console using Unicode? - c#

I'm learning at a nice pace right now trying to rekindle what I almost forgot in programming. Right now I'm re-reviewing the basics of C# coding and I came across an interesting subject on ascii and unicode. The instructor, who I'm learning from online of course, mentioned you can make symbols with either. So I figured, heck yeah I'll do that. I wanted to make a symbol, in Console while using C# with Visual Studio, that shows a capricorn symbol, followed by a heart, and a gemini symbol. Basically saying I (the Capricorn) love(the heart) my girlfriend (The Gemini) using symbols. I assumed it was unicode. However, when I do try to write the line, I get boxes with question marks on all accounts. Here is my code:
using System;
using System.Text;
class EntryPoint
{
static void Main()
{
Console.InputEncoding = Encoding.UTF8;
Console.OutputEncoding = Encoding.UTF8;
//Changes the input and output of the console's encoding
char theGeminiCharacter = '\u264A';
char theCapricornCharacter = '\u2651';
char theHeartCharacter = '\u2764';
string freeSpace = " ";
//Can't seem to find the right unicode process to make the zodiac signs.
Console.WriteLine(theCapricornCharacter + freeSpace + theHeartCharacter + freeSpace + theGeminiCharacter);
}
}
So, what am I doing wrong here? Is it even supposed to be unicode or is there some other code type? I've heard of Alt+X but I didn't really understand it. Thanks in advance!

Yes. It is answered. I noticed that certain fonts just don't do all of the UTF08 Input/Outputs, This particular one (DejaVu Sans Mono) did indeed work. The issue was just font types. Just not sure how to choose that my question was solved.

Related

How to change the encoding of a Process's Standard Input in C#?

I am trying to write to the Standard Input of a Process (a C++ program) in C#. The problem is that the C++ program seems to not be able to read what I write from my C# app. Here are both of the programs:
// C#
static void Main(string[] args) {
Console.InputEncoding = Encoding.Unicode;
Console.OutputEncoding = Encoding.Unicode;
Process CoutN = new Process();
CoutN.StartInfo.FileName = #"C:\Users\Vesk\Desktop\CoutN.exe";
CoutN.StartInfo.UseShellExecute = false;
CoutN.StartInfo.RedirectStandardInput = true;
CoutN.StartInfo.RedirectStandardOutput = false;
CoutN.Start();
CoutN.StandardInput.WriteLine("5");
CoutN.WaitForExit();
Console.ReadKey();
}
// C++
int main() {
int n;
std::cin >> n;
std::cout << "N is " << n << std::endl;
return 0;
}
Here is the output from my C# program: N is 0. It should instead be N is 5.
From what I could understand the encoding that C# uses for a Process's Input/Output Stream is the same as the one used by the Console. Here's the thing though, in my C# program I'll need to be able to use the Console to input cyrillic text (as well as other non-ASCII text). The only Console Encoding which seems to work for that is Encoding.Unicode (which apparently is just UTF-16 LE).
But it seems that when I try to write to the Process's Input Stream, it just can't understand that and it outputs N is 0 for some reason. From what I understand this is because UTF-16 is not compatible with ASCII (or whatever the C++ iostream expects).
So is there a way to actually change the Encoding that the Process's Standard Input uses?
Here is what I have tried/cannot try:
I don't want to change the Console's Encoding, because I plan on running the Process in an asynchronously running method and I want to still be able to interface with the C# program, so I fear that it will mess things up, even if I just change the Encoding, only while writing to the Process and then change it back again.
I know that the C++ program could probably be changed so that it can read UTF-16, but I don't want to do that since my C# program will have to be able to run and "interface" with all kinds of C++ programs, which use the simple iostream (cin, cout). (Edit for clarification: My C# program will only ever need to "send" ASCII text to the C++ program)
I did try to Change the Console's Encoding to Encoding.ASCII and it worked. It also worked if I just left the Console at it's default Encoding. But again this will not work for me, because these encodings don't seem to work with Cyrillic.
I tried setting the Console's Input Encoding to Encoding.UTF8 and that didn't work, although I wouldn't have been able to use that anyway, because it doesn't seem to work with Cyrillic text in my C# app.
I tried to change CoutN.StandardInput.Encoding, but unfortunately it is a read-only property.
I tried to also redirect the Standard Output, just to see if it works, and weirdly enough it does. I can just use CoutN.StandardOutput.ReadToEnd() and it works and the output from the C++ program is read correctly (still says N is 0 though). But for some reason the Standard Input is different.
I tried to just not redirect the Standard Input, again just to see if it works, and again very weirdly it did!. I just wrote 5 in the C# program's Console and the output was N is 5.
I tried to create a StreamWriter with a different encoding and write from there:
StreamWriter str = new StreamWriter(CoutN.StandardInput.BaseStream, Encoding.ASCII);
str.WriteLine("5");
But this didn't work either. I even tried basically all the Encodings and even no Encoding, but nothing worked.
I tried writing to the BaseStream myself:
byte[] buffer = Encoding.ASCII.GetBytes("5");
CoutN.StandardInput.BaseStream.Write(buffer, 0, buffer.Length);
CoutN.StandardInput.WriteLine();
And that didn't work.
At this point I feel like I've exhausted all the options that I could try. Any help is appreciated, thanks in advance!
You don't get to chose what encoding the other process is using from your own code - if you're sending input to the standard input stream of an application that is expecting a specific encoding, then short of it having some setting to change that there's nothing you can do about it.
If you're interacting with the STDIO streams of another application, you are expected to match whatever encoding it is expecting, rather than the other way around. The encoding of the console doesn't really matter except in cases where the application inherits its encoding from the environment.
Indeed, if you're trying to use an encoding that supports Cyrillic characters, then the receiving application must have been designed to accommodate this. If the application was written by someone whose native character set only uses Latin characters then they may well have never bothered to do this. It's part of the reason for the push to adopt UTF8 everywhere since then you don't really need to think about it too much.
Edit: If you're trying to match the expected input encoding, then you do it thusly:
Process.StandardInput.Write(Encoding.ASCII.GetBytes("My String Here\r\n"));
Replacing Encoding.ASCII with whatever character set applies to get the appropriate GetBytes() method.
Note that the ASCII character set specifically does not include Cyrillic characters, so it's impossible to use them in an application that expects ASCII. If it expects some other character set that does support them then you're fine. Cyrillic uses Codepage 855 or needs UTF8.

How to correctly encode arabic subtitles in c#?

Hello there i am creating a video player with subtitles support using MediaElement class and SubtitlesParser library, i faced an issue with 7 arabic subtitle files (.srt) being displayed ???? or like this:
I tried multiple diffrent encoding but with no luck:
SubtitlesList = new SubtitlesParser.Classes.Parsers.SubParser().ParseStream(fileStream);
subLine = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(subLine));
or
SubtitlesList = new SubtitlesParser.Classes.Parsers.SubParser().ParseStream(fileStream,Encoding.UTF8);
Then i found this and based on the answer i used Encoding.Default "ANSI" to parse subtitles then re-interpret the encoded text:
SubtitlesList = new SubtitlesParser.Classes.Parsers.SubParser().ParseStream(fileStream, Encoding.Default);
var arabic = Encoding.GetEncoding(1256);
var latin = Encoding.GetEncoding(1252);
foreach (var item in SubtitlesList)
{
List<string> lines = new List<string>();
lines.AddRange(item.Lines.Select(line => arabic.GetString(latin.GetBytes(line))));
item.Lines = lines;
}
this worked only on 4 files but the rest still show ?????? and nothing i tried till now worked on them, this what i found so far:
exoplayer weird arabic persian subtitles format (this gave me a hint about the real problem).
C# Converting encoded string IÜÜæØÜÜ?E? to readable arabic (Same answer).
convert string from Windows 1256 to UTF-8 (Same answer).
How can I transform string to UTF-8 in C#? (It works for Spanish language but not arabic).
Also am hoping to find a single solution to correctly display all the files is this possible ?
please forgive my simple language English is not my native language
i think i found the answer to my question, as a beginner i only had a basic knowledge of encoding till i found this article
What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text
Your text editor, browser, word processor or whatever else that's trying to read the document is assuming the wrong encoding. That's all. The document is not broken , there's no magic you need to perform, you simply need to select the right encoding to display the document.
I hope this helps anyone else who got confused about the correct way to handel this, there is no way to know the files correct encoding, only the user can.

Overline in console programming

Just a quick question. For the roman numerals above 3999, we used to represent them using a overline. For representing 4000, we will be using the following:
So, to display this overline, what should I do? Also, please advice me which of the above is right?
Update #1
I saw somewhere that we can use Unicode Characters by using the following code:
Console.OutputEncoding = System.Text.Encoding.Unicode;
Console.WriteLine("H\u0305");
Console.WriteLine("\u0305H");
Console.ReadLine();
After putting this code, I have set my console to use Consolas font at 14pt. I got this output:
I was expecting either of the code to show me a combined version, but no avail.
In the console? You can't. Drawing to the console in such a manner is not possible as the console only supports characters.
One does exist in Unicode (as seen here) but this is merely an overlined space.

How to output windows Alt keycodes in a C# console app

How can I output Windows Alt key codes to the console in a C# console app using Console.WriteLine()?
I would like to output characters such as those used for creating boxes.
I can do so manually in a command prompt by holding alt and typing in the appropriate number such as Alt+205, Alt+187, etc.
Thanks
I suppose the easiest way would be to include them directly in your string literals within your source code:
Console.WriteLine("═╗");
EDIT: I'm sorry - my answer is incorrect. ASCII.GetChars will not work for extended ASCII characters. Thanks to Douglas for correcting me.
I think Douglas's answer is the most direct, but you could also get the character based on the value directly using something like this:
char[] characters = System.Text.Encoding.ASCII.GetChars(new byte[]
{65});
For whatever ASCII code you wanted.

System.IO.StreamWrite and Spanish Characters

I need to write the following string to a txt a File:
SEGS,AUS1,1,0,0,712205,584,8659094,2,NUÑEZ FELIX ARTURO,584
I when I use:
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(#fileSobrantes, true)) {
sw.WriteLine("SEGS,AUS1,1,0,0,712205,584,8659094,2,NUÑEZ FELIX ARTURO,584");
}
I get this in the file
SEGS,AUS1,1,0,0,712205,584,8659094,2,NUÑEZ FELIX ARTURO,584
I try with the Encoding parameters in ASCII, UNICODE and ALL UTF and does not work.
System.IO.StreamWriter(#fileSobrantes, true,Encoding.UTF32 ))
You can't easily (and accurately) represent what you get in the file without giving a hex dump. What are you trying to use to read the file? My guess is that if you try Encoding.Default that will work for you, but it's hard to say for sure without knowing what you're trying to use to read it.
The other alternative is that your source string is incorrect. If you've really got it as a string literal in your source code, are you sure you've got Visual Studio set up to interpret it correctly?
See my unicode debugging page for suggested techniques.
EDIT: By the way, why are you prefixing fileSobrantes with #? For identifiers you only need to do that if they're keywords. You may be getting confused with verbatim string literals - but this isn't a string literal, it's a variable name.

Categories