Ver Mensaje Individual
  #1  
Antiguo 11-09-2006
Avatar de ixMike
ixMike ixMike is offline
Miembro
 
Registrado: feb 2004
Posts: 1.151
Reputación: 22
ixMike Va por buen camino
ANSI, UNICODE, Big Endian y UTF-8

Hola, amigos,

El otro día estaba intentando hacer un código para extraer el texto de un archivo UNICODE y ponerlo en un TMemo (por ejemplo). Usé los archivos de C:\WINDOWS\inf para probar. El resultado fue exitoso, pero surgieron nuevas dudas:

Diferencia entre UNICODE, UNICODE Big Endian y UTF-8

Además, intenté hacer una función inverse (guardar el texto de un TMemo en un archivo UNICODE). La función está casi hecha, pero tiene un fallo. Aquí estan las dos:

DE ARCHIVO UNICODE A TEXTO ANSI:

Código Delphi [-]
Function UnicodeToAnsi(FileName: String): String;
 
type //Bloque de lectura de 256 KB
  TBloque = array [0..131071] of WideChar;
 
var
  F : File of TBloque;
  F2: File of WideChar;
  C : TBloque;
  C2: WideChar;
  P : PWideChar;
  Max, Posi, N: Integer;
  Fin, S: String;
 
begin
//Inicializar
Result:='';
S:='';
Fin:='';
Max:=0;
Pos:=0;
 
//Comprobaciones
If not FileExists(FileName) then
  begin
  raise Exception.Create('El archivo indicado no existe');
  exit;
  end;
 
//Calcular tamaño máximo
AssignFile(F2, FileName);
Reset(F2);
Max:=FileSize(F2);
CloseFile(F2);
 
//Si el archivo está vacío, salimos
If Max=0 then exit;
 
//Leer grandes bloques
AssignFile(F, FileName);
Reset(F);
If FileSize(F)>0 then
 for n:=1 to FileSize(F) do
   begin
   Seek(F, n-1);
   Read(F, C);
   P:=C;
   WideCharToStrVar(P, S);
   Fin:=Fin+S;
   Fin:=Copy(Fin, 1, Length(Fin)-1);
   end;
Posi:=FileSize(F)*131072;
CloseFile(F);
 
//Si con los bloques lo hemos leído todo
if Posi=Max then
  begin
  Result:=Copy(Fin, 2, Length(Fin)-1);
  Exit;
  end;
 
//Si quedan datos por leer
AssignFile(F2, FileName);
Reset(F2);
for n:=Posi to Max-1 do
  begin
  Seek(F2, n);
  Read(F2, C2);
  S:=C2;
  Fin:=Fin+S;
  end;

CloseFile(F2);
 
//Damos el resultado final
Result:=Copy(Fin, 2, Length(Fin)-1);
end;


GUARDAR TEXTO ANSI EN ARCHIVO UNICODE

Código Delphi [-]
 
Procedure AnsiToUnicode(Text, FileName: String);
 
type
  TBloq = array[0..8191]of WideChar;
 
var
  F : File of TBloq;
  F2: File of WideChar;
  B : TBloq;
  W : WideChar;
  S : String;
  Max, Posi, N: Integer;
 
begin
//Inicializar
Posi:=0;
Max:=Length(Text);
If Max=0 then Exit; //Salimos si no hay texto
 
//Empezamos a escribir el archivo
AssignFile(F,FileName);
ReWrite(F);

//Si podemos usar los bloques
If Max>8191 then for N:=1 to Max div 8192 do
  begin
  If N=0 then
    begin
    S:='_'+Copy(Text,1,8191);
    StringToWideChar(S,@B,8192);
    B[0]:=WideChar($FEFF);
    Write(F,B);
    Inc(Posi,8192);
    end
   else
    begin
    S:=Copy(Text,(N-1)*8191+1,8192);
    StringToWideChar(S,@B,8192);
    Write(F,B);
    Inc(Posi,8192);
    end;
  end;
 
//Si ya hemos acabado cerramos el archivo
if Posi=Max then
  begin
  CloseFile(F);
  Exit;
  end;
 
//Si no hemos acabo, seguimos
CloseFile(F);
AssignFile(F2,FileName);
Reset(F2);
Seek(F2,FileSize(F2));
If Posi=0 then
  begin
  W:=WideChar($FEFF);
  Write(F2,W);
  end;
For N:=Posi to Max do
  begin
  W:=WideChar(Text[n]);
  Write(F2,W);
  end;
 
//Finalizamos
CloseFile(F2);
end;


¿Alguien podría ayudarme a acabar la segunda función?
¿Alguien podría explicarme la diferencia entre UNICODE, Big Endian y UTF-8, y cómo cargar estos archivos en un TMemo?

Muchísimas gracias.
Responder Con Cita