조회 수 33382 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
131 Android adb로 db보기 2012.08.31 16422 0
130 개념 블루투스 버전별 차이 1 2012.08.23 40602 0
129 C# 에디트 박스 제일 밑으로 스크롤 하기 2012.08.21 15015 0
128 개념 SPDY(스피디) : 더 빠른 웹을 위한 실험적인 프로토콜 2012.08.21 15070 0
127 Android [GIT 사용법] 다른 branch와의 차이점 찾아보기 2012.08.16 21733 0
126 Android Android Property 사용하기 2012.07.17 29655 0
125 개념 UICC와 USIM file 2012.07.13 24522 0
124 LINUX 서버간 폴더 또는 파일을 이동 하는 scp 명령어 2012.06.27 26028 0
123 C# VS2008 C# 응용프로그램 배포버젼(설치버젼) 만들기 2012.06.14 18563 0
» C# C#에서 파일 읽기 (File Read, Write) 2012.06.11 33382 0
121 C# 공유데이터 잠금. Critical Section 사용, lock() 함수 2012.06.11 19925 0
120 Android Android Log (logcat) 보기 2012.06.08 26981 0
119 일반 CSS 참고 사이트 2012.06.05 15336 0
118 LINUX du 명령어 사용법(디스크 용량 확인) 1 2012.05.31 77438 0
117 Android How to create OMA DRM v1.0 content 2012.05.24 13149 0
목록
Board Pagination ‹ Prev 1 ... 21 22 23 24 25 26 27 28 29 30 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5