I`m trying to solve this task : http://www.codeabbey.com/index/task_view/parity-control But as output I get lots of question marks in the website and blank spaces in visual studio console. If I try to print, lets say, 160 as a char (\'u0160') everything works fine, but if I cast int to char I get white space. I searched the internet and tried some conversions from Hexadecimal to char but they work the same way as casting int to char and I get white space again.
Why I get these question marks, do I have to change the encoding or something? Can I create a unicode point from hexadecimal or int and then just do: char output = convertedValue; Here`s my code for the task above:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
string buffer = Console.ReadLine();
string[] container = buffer.Split(' ');
byte[] asciiCodes = new byte[container.Length];
for (int i = 0; i < container.Length; i++)
{
asciiCodes[i] = byte.Parse(container[i]);
}
for (int i = 0; i < asciiCodes.Length; i++)
{
byte currNumber = asciiCodes[i];
string binaryRepresent = Convert.ToString(currNumber, 2).PadLeft(8, '0');
int counter = 0;
for (int j = 0; j < binaryRepresent.Length; j++)
{
if(binaryRepresent[j] == '1')
{
counter++;
}
}
if(counter % 2 == 0)
{
char output = Convert.ToChar(currNumber);
Console.Write(output);
}
}
}
}
You are doing everything right except for:
u0160
is represented in hex format, that means 160 hex == 352 decimal
so if you run
Convert.ToChar(352);
you'll get Š
.
Whereas Convert.ToChar(160)
is resolving unicode symbol u00A0
(A0 hex = 160 dec), that symbol is a "No-break space"
and you see a whitespace.
if you need code to convert from hex string and vice versa, here's how to do it:
string s = "00A0";
//to int
int code = int.Parse(s, System.Globalization.NumberStyles.HexNumber);
//back to hex
string unicodeString = char.ConvertFromUtf32(code).ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With