2014-04-18 1 views
1

이 작업을 해결하려고합니다. http://www.codeabbey.com/index/task_view/parity-control 출력으로 웹 사이트 및 비주얼 스튜디오 콘솔의 빈 공간에 많은 물음표가 표시됩니다. 인쇄하려고하면 160, char (\ 'u0160')로 모든 작동합니다, 괜찮아요,하지만 int 숯불 캐스팅 경우 공백 얻을. 인터넷을 검색하여 16 진수의 변환을 char로 시도했지만 int를 char로 변환하는 것과 같은 방식으로 작업하고 공백을 다시 얻습니다.정수 또는 16 진수 값을 유니 코드 포인트로 변환하십시오.

왜 이러한 물음표가 표시됩니까? 인코딩 등을 변경해야합니까? 16 진수 또는 int에서 유니 코드 포인트를 만든 다음 다음을 수행 할 수 있습니다. char output = convertedValue; Here`s 위의 작업에 대한 내 코드 :

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

     } 
    } 
} 

답변

1

당신이 바로 제외 최선을 다하고 있습니다 :

u0160이 진수 형식으로 표현된다, 즉 160 육각 == 352 진수

그래서 의미 뛰는 경우

Convert.ToChar(352); 

Š을 얻을 수 있습니다.

Convert.ToChar(160)은 유니 코드 기호 u00A0 (A0 16 진수 = 160 dec)을 해결하지만 해당 기호는 "No-break space"이며 공백이 표시됩니다. 당신이 16 진수 문자열와 그 반대로 변환하는 코드를 필요로하는 경우

, 여기에 그 방법은 다음과 같습니다

string s = "00A0"; 
//to int 
int code = int.Parse(s, System.Globalization.NumberStyles.HexNumber); 
//back to hex 
string unicodeString = char.ConvertFromUtf32(code).ToString(); 
+0

감사 남자, 작동 :) – Vallerious

관련 문제