Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Integer or Hexadecimal value to unicode point

Tags:

c#

unicode

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);
            }

        }
    }
}
like image 740
Vallerious Avatar asked Apr 18 '14 09:04

Vallerious


1 Answers

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();
like image 109
Andrew Avatar answered Sep 26 '22 03:09

Andrew