조회 수 33427 댓글 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
번호 분류 제목 날짜 조회 수 추천 수
506 LINUX awk 명령어 사용법 1 2006.02.16 114752 15
505 MFC CString 에서 형변환 함수들 총정리 2010.11.29 102799 0
504 Android adb am 명령어 ; app 실행 및 Intent 전송 2013.08.12 100462 0
503 Android [GIT 사용법] Git Tutorial 2011.12.26 96574 0
502 일반 ┗ bat(배치)파일 문법 2007.08.06 93676 8
501 LINUX [Shell Script] 리눅스 쉘(Shell) 스크립트 2014.09.23 86878 0
500 JAVA JAVA String 클래스 메소드 정리 1 2015.02.05 85028 0
499 LINUX [Shell Script] 쉘 스크립트에서의 사칙연산과 문자열 자르기 2014.11.01 81826 0
498 C# StringBuilder로 문자열 처리를 빠르게 2012.12.04 78010 0
497 LINUX du 명령어 사용법(디스크 용량 확인) 1 2012.05.31 77518 0
496 Python BeautifulSoup으로 웹에 있는 데이터 긁어오기 2013.04.08 77015 0
495 Android [GIT 사용법] 초보자가 알아두면 좋을 명령어 정리 1 2011.12.26 66600 0
494 LINUX errno.h - system error numbers 2013.01.09 65957 0
493 일반 findstr 사용법 - window용 find, grep 명령 2014.02.04 63206 0
492 MFC API를 이용하는 유니코드와 ANSI 문자열간의 변환 방법 2006.04.14 63206 0
목록
Board Pagination ‹ Prev 1 2 3 4 5 6 7 8 9 10 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


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

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

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5