UTF8 encoding for VB.NET program

I try to create a textfile in a XOJO program which should be readable with a VB.NET program. The strings are encoded to UTF8.
The string sRandom also contains bytes with values between chr(50) and chr(255).

When i read the file in the VB.net program it fails to correctly read it. When i open the textfile in notebook (Windows) en save the file to encoding UTF8 it works again. ???
With the code below I think to write the file to a UFT8 encoding with XOJO, but Windows programs don’t recognize it as UFT8??
What am I doing wrong?


Dim f As FolderItem
f = GetSaveFolderItem("", “licence.dat”)
Dim SaveStream As TextOutputStream = TextOutputStream.Create(f)

dim cr as string
cr = ENDOFLINE.WINDOWS

SaveStream.Write(convertencoding(“LICENCEFILE: DO NOT CHANGE ANYTHING OR ELSE THE LICENCE IS INVALLID” + cr, Encodings.UTF8))
SaveStream.Write(convertencoding(sProg + cr, Encodings.UTF8))
SaveStream.Write(convertencoding(sName + cr, Encodings.UTF8))
SaveStream.Write(convertencoding(sAdress + cr, Encodings.UTF8))
SaveStream.Write(convertencoding(sRandom + cr, Encodings.UTF8))
SaveStream.Close()

[quote=174092:@Coen Martinus]SaveStream.Write(convertencoding(“LICENCEFILE: DO NOT CHANGE ANYTHING OR ELSE THE LICENCE IS INVALLID” + cr, Encodings.UTF8))
SaveStream.Write(convertencoding(sProg + cr, Encodings.UTF8))
SaveStream.Write(convertencoding(sName + cr, Encodings.UTF8))
SaveStream.Write(convertencoding(sAdress + cr, Encodings.UTF8))
SaveStream.Write(convertencoding(sRandom + cr, Encodings.UTF8))
SaveStream.Close()[/quote]

You should not need to convertEncoding. It may actually be the source of the issue. Xojo strings are UTF8 as default. Look at the example of creating text files at http://documentation.xojo.com/index.php/TextOutputStream

Also, using WriteLine adds automatically the proper endOfLine character.

As default, the examples listed create plain text files identical to what Notepad would save. Have you tested your VB program with files created with Notepad ?

Even if I remove the convertencoding Xojo doesn’t write the file in a correct way.

The goal is the following textfile:

LICENCEFILE: DO NOT CHANGE ANYTHING OR ELSE THE LICENCE IS INVALLID
MT940 Creator
Test BV (Coen M)
Streetnaam 6, 3333 KL Cityname, Country
œ’SÜê°ä¼€œ]¬:áqŸÅˆÞ÷

The last line is a key that is generated. This causes the problem.

When I create the file in a VB.NET application then UTF8 is correctly applied with two bytes for ascii codes above 127. XOJO doen’t write the bytes higher then asc(127) in two bytes to the textfile. (I have to manualy re-save it as UTF8 in notebook to get it correctly). This are the difference for the last line when I open the textfiles in a hexeditor.

In hex my vb.net application would create (or after manualy resave it as UTF8 with notebook):
C5 93 E2 80 99 53 C3 9C C3 AA C2 B0 C3 A4 C2 BC E2 82 AC C2 8F C5 93 5D C2 AC 3A C3 A1 71 C5 B8 C3 85 CB 86 C3 9E C3 B7

While XOJO writes the last line away like:
9C 92 53 DC EA B0 E4 BC 80 8F 9C 5D AC 3A E1 71 9F C5 88 DE F7

With this I get a file with accented characters coded on two bytes :

Dim t As TextOutputStream Dim f As FolderItem f = GetSaveFolderItem("", "CreateExample.txt") If f <> Nil Then t = TextOutputStream.Create(f) t.WriteLine(ConvertEncoding(TextField1.text, Encodings.UTF8)) t.Close End If

gives

c3 a2 c3 aa c3 ae c3 b4 c3 bb

If i copy paste the last line in a textbox for example the adress it looks like it converts to UTF8.

In my program the last line is instead of entered in a textbox it is calculated. The calculated bytes are stored in the string sRandom.

sRandom = chr(xx) + chr(xx) + chr(xx)

Is it possible that the ConvertEncoding only works with textboxes and not with a generated string?

I tried with this :

dim randomstring as string = "âêîôû" t.WriteLine(ConvertEncoding(randomstring, Encodings.UTF8))

Same result.

I think you can pretty safely adapt the snippet I posted for your own use. Notice I use WriteLine, which saves the use of cr and the need to define it.

[quote=174244:@Coen Martinus]In hex my vb.net application would create (or after manualy resave it as UTF8 with notebook):
C5 93 E2 80 99 53 C3 9C C3 AA C2 B0 C3 A4 C2 BC E2 82 AC C2 8F C5 93 5D C2 AC 3A C3 A1 71 C5 B8 C3 85 CB 86 C3 9E C3 B7

While XOJO writes the last line away like:
9C 92 53 DC EA B0 E4 BC 80 8F 9C 5D AC 3A E1 71 9F C5 88 DE F7[/quote]
You’re not creating the string in the same way. Look at the code that creates the string. That is where you error is, not in writing it to the file.