소스파일 자체의 인코딩도 UTF-8이고 페이지세팅도 UTF-8일 경우,
아래와 같은 형태로 가져오면 됨.


function getURLContent(URL: String): String;
var
  IdHTTP1: TIdHTTP;
  Stream: TBytesStream;

begin
  Result:='';
  Stream := TBytesStream.Create;
  IdHTTP1:=TIdHTTP.Create(nil);
  try
    IdHTTP1.Get(URL, Stream, []);
    Result:=TEncoding.UTF8.GetString(Stream.Bytes, 0, Stream.Size);
  finally
    FreeAndNil(IdHTTP1);
    FreeAndNil(Stream);
  end;
end;







중요한 것은 TEncoding



아래와 같은 property 를 가지고 있으므로 인코딩에 따라 적절하게 사용하면 될 듯.

TEncoding.ASCII                   - Returns an encoding for the ASCII character set.  
TEncoding.BigEndianUnicode - Returns an encoding for the UTF-16 format with big-endian byte order.  
TEncoding.Default                 - Returns an encoding for the operating system`s default ANSI code page.  
TEncoding.Unicode               - Returns an encoding for the UTF-16 format with little-endian byte order.  
TEncoding.UTF7                   - Returns an encoding for the UTF-7 format.  
TEncoding.UTF8                   - Returns an encoding for the UTF-8 format.







아래는 RAD Studio VCL Reference 의 예제

{
This example determines the encoding of a source file and then
write the file in any encoding chosen.  Use this project to
create files of any encoding.  This example requires two text edits
and a combobox.
}

procedure TForm1.Button1Click(Sender: TObject);
var
  LBuffer: TBytes;
  LByteOrderMark: TBytes;
  LOffset: Integer;
  LEncoding, DestEncoding: TEncoding;
  LFileStream: TFileStream;
  EncodingArray: array[0..5] of TEncoding;
begin
  LEncoding:= nil;
  EncodingArray[0]:= TEncoding.UTF8;
  EncodingArray[1]:= TEncoding.UTF7;
  EncodingArray[2]:= TEncoding.Unicode;
  EncodingArray[3]:= TEncoding.Default;
  EncodingArray[4]:= TEncoding.BigEndianUnicode;
  EncodingArray[5]:= TEncoding.ASCII;
  DestEncoding := EncodingArray[ComboBox1.ItemIndex];
  LFileStream := TFileStream.Create(Edit1.Text, fmOpenRead);
  try
    // Read file into buffer
    SetLength(LBuffer, LFileStream.Size);
//    LFileStream.Read(LBuffer[0], Length(LBuffer));
    LFileStream.ReadBuffer(Pointer(LBuffer)^, Length(LBuffer));

    // Identify encoding and convert buffer to UTF8
    LOffset := TEncoding.GetBufferEncoding(LBuffer, LEncoding);
//    for I := 0 to Length(LBuffer) - 1 do
//      Memo1.Lines.Add((LBuffer[I]));
    LBuffer := LEncoding.Convert(LEncoding, DestEncoding, LBuffer,
      LOffset, Length(LBuffer) - LOffset);
  finally
    LFileStream.Free;
  end;

  LFileStream := TFileStream.Create(Edit2.Text, fmCreate);
  try
    // Write an encoding byte order mark and buffer to output file
    LByteOrderMark := DestEncoding.GetPreamble;
    LFileStream.Write(LByteOrderMark[0], Length(LByteOrderMark));
    LFileStream.Write(LBuffer[0], Length(LBuffer));
  finally
    LFileStream.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  StringList: TStrings;
begin
  StringList := TStringList.Create;
  try
    with StringList do begin
      Add('TEncoding.UTF8');
      Add('TEncoding.UTF7');
      Add('TEncoding.Unicode');
      Add('TEncoding.Default');
      Add('TEncoding.BigEndianUnicode');
      Add('TEncoding.ASCII');
    end;
    with ComboBox1 do begin
      Items.Assign(StringList);
      ItemIndex := 0;
    end;
  finally
    StringList.free;
  end;
end;













Posted by bloodguy

댓글을 달아 주세요