본문 바로가기
Delphi Tip/문자

델파이 UTF-8

by MonoSoft 2023. 11. 15.
728x90
반응형

델파이 UTF-8

728x90

 

 

UTF-8

 

UCS Transferation Format 의 약자로 Unicode를 위한 가변 길이 문자 인코딩 방식 중 하나이다.

Unicode는 전 세계의 모든 문자를 표현하기 위한 표준을 제공하는 국제적인 문자 집합이다.

UTF-8은 이러한 Unicode 문자를 바이트로 효과적으로 표현하는 방법 중 하나로,

많은 컴퓨터 시스템에서 표준으로 사용되고 있다.

 

 

 

 

UTF-8 특징

 

가변 길이 인코딩: UTF-8은 각 문자를 1바이트부터 4바이트까지 가변 길이로 인코딩한다.

기본적으로 ASCII 문자는 1바이트로 인코딩되며,

이는 기존의 ASCII 문자 집합과의 하위 호환성을 제공한다.

 

다른 언어의 문자는 더 많은 바이트로 표현된다.

ASCII 호환성: UTF-8은 ASCII 문자 집합과 완벽하게 호환된다.

 

이는 기존의 ASCII 기반 시스템에서도 문제 없이 사용할 수 있도록 한다.

유니코드 호환성: UTF-8은 Unicode를 기반으로 하며,

Unicode의 모든 문자를 효과적으로 표현할 수 있다.

 

문자 인코딩 효율: 일반적으로 텍스트 문서에서는 영어나 기타 대부분의 언어의

문자가 1바이트로 표현되므로, UTF-8은 상당히 효율적인 문자 인코딩 방식이다.

 

UTF-8은 웹 페이지, 이메일, 데이터 저장 등 다양한 컴퓨터 응용 분야에서 널리 사용되며,

현재 대부분의 운영 체제와 프로그래밍 언어에서 지원된다.

 

 

 

인코딩

 

1bytechar(Unicode) -> 1,2,3,4,5,6 char 로 인코딩하는 방법이다.

1byte char는 비트표시로 0xxxxxxx 으로 나타내어지고 이는 Unicode < 127 일때 인코딩 된다.

이땐 그냥 Unicode = UTF8 이다.

다음은 Unicode 128 ~ 255 사이 값일때 이다.

요건... 2bytechar 로 나타낸다.

그러므로

110000xx 10xxxxxx 으로 나타낸다.

다음은 한글!

대부분 외산 MSN 연동 메신저가 한글을 지원하지 않는 까닭은

바로 한글이 2Byte 글자이기 때문입니다.

 

따라서리

 

1110xxxx 10xxxxxx 10xxxxxx 으로 UTF8 형식으로 변환된다

그런데 그냥 String 값을 char 나누어 쪼개서 넣으면 안된다.

윈도우에서 쓰는 한글코드를 Unicode 로 바꾸어야한다

 

 

변환 코드 예제

procedure StrtoUtf8(var str : string;const Flag : boolean = True); var

n : integer;

RStr: string;

SIndex : integer; buff : array [0..2] of WideChar; c,d : char;

unicode : word;

begin

RStr := '';

if Flag then

begin

SIndex := pos(' ',Str); while SIndex > 0 do begin

Delete(Str,SIndex,1); Insert('%20',str,SIndex); SIndex := pos(' ',str); end;

end;

n := 1;

while n <= length(Str) do begin

c := str[n];

if not IsDBCSLeadByte(Byte(c)) then begin

if (ord(c) < 128) then begin

RStr := RStr + c; end

else

begin

RStr := RStr + char( (ord(c) shr 6 ) or 192 ); RStr := RStr + char( (ord(c) and 63) or 128 ); end;

INC(n);

end

else

begin

INC(n);

d := str[n];

StringToWideChar(c+d,buff,sizeof(buff)); unicode := word(buff[0]); c := char(unicode shr 8); d := char(unicode and 255); Rstr := Rstr + char( (ord(c) shr 4) or 224 ); c := char(ord(c) and 15); c := char(ord(c) shl 2); RStr := Rstr + char( (( ord(d) shr 6 ) or ord(c) ) or 128 ); RStr := Rstr + char( (ord(d) and 63) or 128); Inc(n);

end;

end;

str := RStr;

end;

 

 

procedure Utf8toStr(var utf8 : string;const Flag : boolean = True); var

SIndex,n : integer; TempString : string; AscCode : word;

buff : array [0..2] of WideChar; c,d,e : char;

unicode : word;

begin

if utf8 = '' then Exit;

if Flag then

begin

SIndex := pos('%',utf8); while SIndex > 0 do begin

TempString := Copy(utf8,SIndex+1,2); Delete(utf8,SIndex,3); AscCode := StrToInt('$'+TempString); TempString := char(AscCode); Insert(TempString,utf8,SIndex); SIndex := pos('%',utf8); end;

end;

n :=1;

TempString := ''; while n <= length(Utf8) do begin

c := utf8[n];

if (ord(c) and 128) = 0 then begin

TempString := TempString + c; Inc(n);

end

else if (ord(c) and 224) <> 224 then begin

Inc(n);

d := utf8[n];

TempString := TempString + char( ((ord(c) shl 6) and 255) or (ord(d) and 63)); Inc(n);

end

else

begin

Inc(n);

d := utf8[n];

Inc(n);

e := utf8[n];

Unicode := (( ((ord(c) shl 4) and 255) or ((ord(d) and 60) shr 2) ) shl 8) or ( ((ord(d) and 3) shl 6) or (ord(e) and 63) ); buff[0] := WideChar(Unicode); buff[1] := WideChar(0); TempString := tempString + WideCharToString(buff); Inc(n);

end;

end;

Utf8 := TempString;

end;

 

728x90
반응형

'Delphi Tip > 문자' 카테고리의 다른 글

한글 초/중/종성 알아내기  (0) 2024.02.15
Case문으로 문자 비교하기  (0) 2023.11.08
String 을 PAnsiChar 변환  (0) 2021.10.14
난수 문자열 만들기  (0) 2021.10.13
문자 사이에서 숫자 추출하기  (0) 2021.10.12

댓글