none
File.WriteContents(Text.GetCharacter(255)) creates garbage... RRS feed

  • Question

  • Hello,

    I am trying to write the letter ÿ (Hex code FF, ASCII 255) to a file, using the Small Basic File.WriteContents function as follows:

    File.WriteContents(path+"\test.txt", Text.GetCharacter(255))
    When checking the resulting file in a hex editor the result is:

    C3 BF

    instead of a simple FF as I want it to be...

    Any idea how this can be remedied?

    With kind regards,

    Mario Braun.

    Thursday, November 21, 2013 1:53 PM

Answers

  • I wouldn't call it an encoding problem - more that the default file encoding used is Unicode UTF-8 not ascii.

    Small Basic was not really designed to write hex output I guess.  This would be easy in say C#, but I don't think it is possible with SmallBasic (without an extension) since it does convert to Unicode when writing / reading to files.

    This is the method used by SmallBasic - see here (default encoding is UTF-8).  So internally all file stuff is done with UTF-8.

    See here for C# method or loads of stuff if you google it.

    • Marked as answer by czevak Thursday, November 21, 2013 4:11 PM
    Thursday, November 21, 2013 3:52 PM
    Moderator

All replies

  • If you open the file in notepad you get ÿ, in word pad you get ÿ. I you open with Word you get the choice of encoding:

    Note that ÿ in UTF-8 is C3 BF, and C3 BF in ascii is ÿ.

    So it is actually being stored in Unicode UTF-8 format.

    Thursday, November 21, 2013 3:10 PM
    Moderator
  • Okay...so it is an encoding problem of File.WriteContents.

    The thing is that File.ReadContents does read the file properly, but when writing it again from memory, the FF bytes get garbled.

    A bit of background:

    I want to exchange names and text sequences inside a binary database file, to realize a batch processing program. It should take names and letter sequences from a list and save each couple of datasets into a file template which is used by another Windows program.

    The template gets read with File.ReadContents and then seperated into fixed character blocks that need to stay the same (including FF hex bytes) and the two text parts that get exchanged.

    You see my problem? The changes when saving the file back that happens to the FF hex bits crashes the subsequent windows program trying to load the file.

    So can I force WriteContent to save in ASCII encoding?

    Thursday, November 21, 2013 3:22 PM
  • I wouldn't call it an encoding problem - more that the default file encoding used is Unicode UTF-8 not ascii.

    Small Basic was not really designed to write hex output I guess.  This would be easy in say C#, but I don't think it is possible with SmallBasic (without an extension) since it does convert to Unicode when writing / reading to files.

    This is the method used by SmallBasic - see here (default encoding is UTF-8).  So internally all file stuff is done with UTF-8.

    See here for C# method or loads of stuff if you google it.

    • Marked as answer by czevak Thursday, November 21, 2013 4:11 PM
    Thursday, November 21, 2013 3:52 PM
    Moderator
  • Thank you for this clarification.

    Basically I have to switch developing language to something serious or code my own small basic extension that brings ReadAllBytes and WriteAllBytes to small basic...

    Let me see what I can do here. Thank you. :)

    Thursday, November 21, 2013 4:10 PM