조회 수 33381 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

출처 : http://blog.naver.com/sorkelf?Redirect=Log&logNo=40158854917


C#은 기본적으로 UTF-8 방식으로 인코딩 한다

(일반적인 문자들(숫자,영어등)을 사용할 거면 상관없지만.. 그 외 다른 나라 언어라던지..)


using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

//FileStream 생성 후 출력
FileStream fileStreamOutput = new FileStream(strFileName, FileMode.Create);

//파일포인터 처음으로..
fileStreamOutput.Seek(0, SeekOrigin.Begin);
WriteString("한글문자 출력", fileStreamOutput);
WriteString("桜蘭高校ホスト部 高杉晋助", fileStreamOutput);
WriteString("您好,见到您很高兴", fileStreamOutput);

fileStreamOutput.Flush();
fileStreamOutput.Close();

ReadString();
}

//File Read
public static void ReadString()
{
byte[] byteArray = new byte[MAX_BTYE];

FileStream fileStreamInput = File.OpenRead(strFileName);
UTF8Encoding utf8Encoding = new UTF8Encoding(true);

//File Read
while (fileStreamInput.Read(byteArray, 0, byteArray.Length) > 0)
{
str += utf8Encoding.GetString(byteArray);
}
//Console.WriteLine(str);
fileStreamInput.Close();
}

//File Write
public static void WriteString(String str, FileStream fs)
{
byte[] info;

//Use UTF-8 ?
if(_USE_UTF8_INCODING)
info = new UTF8Encoding(true).GetBytes(str);
else
info = System.Text.Encoding.Default.GetBytes(str);

fs.Write(info, 0, info.Length);
}

private const int MAX_BTYE = 1024;
private static string str;
private static string strFileName = "test.txt";

private const bool _USE_UTF8_INCODING = true;
}

}


info = System.Text.Encoding.Default.GetBytes(str);


에서 System.Text.Encoding.Default는 현재 사용중인 운영체제의 ANSI Code Page를 의미한다(ASCII와 다름)


뭐 한국어 윈도우니까 한국어는 깨지지 않겠지만 일본어나 중국어등은 깨져서 나올것이다

(물론 일본 윈도우에 한국어도 깨지듯이 나오듯..)


아래는 UTF8Encoding 클래스..


UTF8Encoding 클래스

.NET Framework 4

유니코드 문자의 UTF-8 인코딩을 나타냅니다.

네임스페이스: System.Text
어셈블리: mscorlib(mscorlib.dll)




물론 그 이외에 다양한 변환법이 있을 것이다..
더 자세한건 MSDN 참조..

변환을 할때에는 System.Text에 Encoding이라는 클래스가 존재하며
이 클래스를 사용하면 문자 <-> UTF-8, 문자 <-> UTF-16, UTF-16 <-> UTF-8
같은 인코딩 변환도 쉽게 할 수 있다.

변환은 아래에 예시처럼 할 수 있다

byte[] defaultBytes = Encoding.Default.GetBytes( text );

byte[] utf8Bytes = Encoding.Convert( Encoding.Default, Encoding.UTF8, defaultBytes );
string resultString = Encoding.UTF8.GetString( utf8Bytes ); ;


어디까지나 이것은 C#에서 가능한 것이며 (ATL 헬퍼 함수들을 사용하면 가능..)

이러한 변환을 C++에서 할 수 있도록 하는 것은 아래에 링크를 참조하길..

http://www.gamedevforever.com/57 <- 놀개영



추가 .. : ATL은 요런게 있음..

void foo( const char *in, char *out, int nOut )

{
USES_CONVERSION;
wchar_t *wc = A2W( in ); // ANSI to UCS-2
WideCharToMultiByte( CP_UTF8, 0, wc, -1, out, nOut, 0, 0 ); // UCS-2 to UTF-8
}


USES_CONVERSION하고 A2W을요 놈이 ATL 관련 함수이다

반대로 변환할 떄는 MultiByteToWideChar ()와 W2A를 사용하면 된다


ANSI나 UTF-8이나 다 MultiByte 이며.

A2W는 내부적으로 MultiByteToWideChar를,

W2A는 내부적으로 WideCharToMultiByte를 호출한다.


Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
191 Android repo 명령어 (command) 설명 init sync diff prune forall upload download start status 2013.05.30 20531 0
190 Android 이클립스(Eclipse)에서 유용한 단축키 일람 1 2013.05.21 39125 0
189 Python Python 문자열 관련 함수 2013.05.07 22571 0
188 Android CPU사용률은 /proc/stat를 참고 2013.05.07 30503 0
187 LINUX 접속한 사용자 확인 w 명령어 2013.05.02 13179 0
186 Android ADB 에서 cpu 사용률 보기 2013.04.29 20132 0
185 C# Textbox에 숫자만 입력가능하게 설정하기 2013.04.22 20803 0
184 LINUX 사용자 계정 목록 보기 2013.04.22 15916 0
183 Android CDMA 모델에서의 IMEI secret 2013.04.22 0 0
182 Android adb 로 low battery 이벤트 날리기 2013.04.19 16751 0
181 C# Quick Sort 함수 2013.04.17 9881 0
180 Python python 문법요약 2013.04.08 28558 0
179 Python BeautifulSoup로 HTML 파싱 끝내기 2013.04.08 42527 0
178 Python BeautifulSoup으로 웹에 있는 데이터 긁어오기 2013.04.08 76928 0
177 일반 자주 사용하는 아주 유용한 파워포인트 단축키 [Useful Short-cut for PowerPoint] 2013.04.01 19948 0
목록
Board Pagination ‹ Prev 1 ... 17 18 19 20 21 22 23 24 25 26 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5