조회 수 33458 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
146 개념 AC3, Dolby Digital file 2012.11.14 13972 0
145 개념 DTS(Digital Theater System) 2012.11.14 14353 0
144 C# 컬렉션(Collection) - ArrayList / HashTable / Queue / Stack 2012.11.12 16102 0
143 Android [GIT] git stash 사용하기 2012.10.22 31287 0
142 일반 Beyond Compare로 patch 파일 만들기, 적용하기 file 2012.10.22 16415 0
141 LINUX 패치 파일 만들기와 적용하기 (patch, diff) 2 2012.10.22 35973 0
140 C# C# 문자열 숫자형식 포맷팅 String.Format ( C# Numberic Formatting ) 2012.10.10 59421 0
139 C 정규식 테스트 사이트 2012.09.20 17504 0
138 일반 DOSKEY 명령어 사용법 2012.09.19 15169 0
137 일반 CMD 창에서 ALIAS 사용하기 2012.09.19 14979 0
136 Android 자바 call stack을 임의로 보는 방법 2012.09.05 17700 0
135 C# C# 코드에서 cmd 명령어기 날리기, 리디렉션하기 2012.09.03 34340 0
134 Android adb shell 을 이용한 각종 정보 명령어 2012.09.03 42325 0
133 Android 안드로이드 어플리케이션의 메모리 사용량을 확인하는 방법 2012.09.03 28846 0
132 일반 SELECT 문 간단 2012.08.31 12784 0
목록
Board Pagination ‹ Prev 1 ... 20 21 22 23 24 25 26 27 28 29 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5