Reading unicode text from an external txt file

Hey there,
how i can read unicode data from an external .txt file to my delphi app.
i have save the file in utf-8 format and i'm using TnTWare as well. But i still get something blah blah in the form.
[code]
Var lngFile:TextFile;
.
.
.
AssignFile(lngFile,'language/'+lngFileAdrs);
Reset(lngFile);
Read(lngFile,Str);
TntButton.Caption:=Str;
[/code]
also i check reading file in binary format, that's not the way.
and also i use WideString instead of String, that's not the way, too.

Thanks in advance;

Comments

  • : Hey there,
    : how i can read unicode data from an external .txt file to my delphi app.
    : i have save the file in utf-8 format and i'm using TnTWare as well. But i still get something blah blah in the form.
    : [code]
    : Var lngFile:TextFile;
    : .
    : .
    : .
    : AssignFile(lngFile,'language/'+lngFileAdrs);
    : Reset(lngFile);
    : Read(lngFile,Str);
    : TntButton.Caption:=Str;
    : [/code]
    : also i check reading file in binary format, that's not the way.
    : and also i use WideString instead of String, that's not the way, too.
    :
    : Thanks in advance;
    :
    I would read the file as a binary, and convert it to a WideString (or string) based on the utf-8 spcification (http://en.wikipedia.org/wiki/Utf-8). This decoding can quite easily be done using the binary operators (and, shr, shl, or, ...).
  • : I would read the file as a binary, and convert it to a WideString (or string) based on the utf-8 spcification (http://en.wikipedia.org/wiki/Utf-8). This decoding can quite easily be done using the binary operators (and, shr, shl, or, ...).

    Thanks for your reply;
    Can you show me an example of reading and converting? I have not any idea about this.
    i would be thankful...
  • : : I would read the file as a binary, and convert it to a WideString (or string) based on the utf-8 spcification (http://en.wikipedia.org/wiki/Utf-8). This decoding can quite easily be done using the binary operators (and, shr, shl, or, ...).
    :
    : Thanks for your reply;
    : Can you show me an example of reading and converting? I have not any idea about this.
    : i would be thankful...
    :
    Here is an untested pseudocode to convert the utf-8 char to a WideChar:
    [code]
    function UTF8ToWideChar(bytes: array of byte; var i: integer): WideChar
    if bytes[i] and 128 = 0 then
    Result = bytes[i]
    inc(i)
    else if (bytes[i] and 128+64 = 128+64) and (bytes[i] and 32 = 0) then
    Result = bytes[i+1] xor 128;
    Result = Result + (bytes[i] and 2+1) shr 6;
    Result = Result + ((bytes[i] and 16+8+4) shr 2)*256;
    inc(i, 2)
    else if (bytes[i] and 128+64+32 = 128+64+32) and (bytes[i] and 16 = 0) then
    Result = bytes[i+2] xor 128;
    Result = Result + (bytes[i+1] and 2+1) shr 6;
    Result = Result + (bytes[i] shl 4 + (bytes[i+1] xor 128) shr 2)*256;
    inc(i, 3)
    end;
    [/code]
    The reading part is up to you, but shouldn't be too hard, when using a TFileStream and an array of bytes.

  • : Here is an untested pseudocode to convert the utf-8 char to a WideChar:
    : [code]
    : function UTF8ToWideChar(bytes: array of byte; var i: integer): WideChar
    : if bytes[i] and 128 = 0 then
    : Result = bytes[i]
    : inc(i)
    : else if (bytes[i] and 128+64 = 128+64) and (bytes[i] and 32 = 0) then
    : Result = bytes[i+1] xor 128;
    : Result = Result + (bytes[i] and 2+1) shr 6;
    : Result = Result + ((bytes[i] and 16+8+4) shr 2)*256;
    : inc(i, 2)
    : else if (bytes[i] and 128+64+32 = 128+64+32) and (bytes[i] and 16 = 0) then
    : Result = bytes[i+2] xor 128;
    : Result = Result + (bytes[i+1] and 2+1) shr 6;
    : Result = Result + (bytes[i] shl 4 + (bytes[i+1] xor 128) shr 2)*256;
    : inc(i, 3)
    : end;
    : [/code]
    : The reading part is up to you, but shouldn't be too hard, when using a TFileStream and an array of bytes.
    :

    Thanks... I'm goin' to try ;)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories