2014-10-29 4 views
0

URL 매개 변수를 취하고 논리를 실행 한 다음 구분 된 값 목록을 다시 전달하는 asp.net GET webservice가 있습니다.UTF8을 Windows-1252로 변환

내가 가진 문제는 Windows-1252 인코딩을 사용하는 요청을 만드는 서비스이므로 요청하는 컴퓨터로 반환 될 때 "문자가 제대로 표시되지 않습니다. UTF8에서 Windows-1252로 문자열을 변환하여 전달하는 빠른 방법을 찾고 있습니다.

+2

이미 * 문자열 *이있는 경우 인코딩이 필요하지 않습니다. 이미 적용되었습니다. 문자열을 얻기 위해 인코딩을 'byte []'에 적용했다가 다시 돌아와야합니다. (또는 한 번에 이것을하기 위해'Encoding.Convert'를 사용하십시오.) 당신이 우리에게 * 어떤 * 코드도 보여주지 않았다는 것을 돕지 않습니다 ... –

답변

1

바이트 배열에 문자열 inputStr 변환 :

byte[] bytes = new byte[inputStr.Length * sizeof(char)]; 
System.Buffer.BlockCopy(inputStr.ToCharArray(), 0, bytes, 0, bytes.Length); 

1252로 변환 :

Encoding w1252 = Encoding.GetEncoding(1252); 
byte[] output = Encoding.Convert(utf8, w1252, inputStr); 

다시 문자열을 가져옵니다 :

w1252.GetString(output); 
+0

이것은 업데이트가 필요합니다. – Sedrick

0

존 소총은 이미 지적한 바와 같이, 문자열 자체에 인코딩이 없으면 인코딩이있는 byte[]입니다. 문자열에 어떤 인코딩이 적용되었는지 알아야합니다.이 경우 문자열에 대해 byte[]을 검색하고 원하는 인코딩으로 변환 할 수 있습니다. 그런 다음 결과로 나온 byte[]을 추가 처리 (예 : 파일에 쓰거나 HttpRequest에서 반환) 할 수 있습니다.

// get the correct encodings 
var srcEncoding = Encoding.UTF8; // utf-8 
var destEncoding = Encoding.GetEncoding(1252); // windows-1252 

// convert the source bytes to the destination bytes 
var destBytes = Encoding.Convert(srcEncoding, destEncoding, srcEncoding.GetBytes(srcString)); 

// process the byte[] 
File.WriteAllBytes("myFile", destBytes); // write it to a file OR ... 
var destString = destEncoding.GetString(destBytes); // ... get the string 
관련 문제