조회 수 33385 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
62 C# XML 간단 예제 2012.01.03 23249 0
61 C# 디렉토리 경로 입력 받기 2010.03.15 38399 0
60 C# C# 레지스트리 사용하기 2010.03.15 40398 0
59 C# C#의 $으로 문자열 보간하기 2022.04.28 3718 0
58 C# 문자열 검색 / 조작 2012.01.03 23411 0
57 C# c# file i/o 샘플 2012.01.04 16943 0
56 C# 문자열 다루기 1 2012.04.06 18497 0
55 C# C# 형변환 (문자형 -> 숫자형) / C# Type Change (string -> number) 2012.04.12 57239 0
54 C# C# ?? 및 ??=, ?. 연산자 2021.05.09 3003 0
53 C# Visual C# 에서 XP 테마 적용하기 2012.05.21 15117 0
52 C# c# 다국어 개발 2012.05.22 18077 0
51 C# [C#] (System.Collections.Generic) ArrayList 2012.05.23 15248 0
50 C# 공유데이터 잠금. Critical Section 사용, lock() 함수 2012.06.11 19927 0
» C# C#에서 파일 읽기 (File Read, Write) 2012.06.11 33385 0
48 C# VS2008 C# 응용프로그램 배포버젼(설치버젼) 만들기 2012.06.14 18565 0
목록
Board Pagination ‹ Prev 1 2 3 4 5 Next ›
/ 5

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5