Coverting text files with binary file page code functionality

Discussion in 'Code Samples & Tips' started by kolbe, Jun 20, 2008.

  1. kolbe

    kolbe Active Member Licensed User

    Hello all,

    I needed to use an a 8-bit ascii (as in with diacritics, accents and the like) file with bassic4ppc so I had to figure out how to use the page code options in the binaryfile object. Don't know if this is the best way to do this but it works. There is not much of an explanation in the help files so thought this might be helpful to someone else.

    tbin1 is the binfile object

    To import a file:

    Code:
    'convert extended ascii to native utf-8
       FileOpen(c1,"ascii file.txt",cRandom)
       FileOpen(c2,
    "utf8 file.txt",cWrite)
       
       
    'use  1252 or 28591 for the ascii page code
       tbin1.New2(c1,1252)
       
       
    'read bytes
       length=FileSize("ascii file.txt")
       
    Dim temp_array(length) As byte
       tbin1.ReadBytes(temp_array(),length)

       
    'convert to string
       string=tbin1.BytesToString(temp_array(),0,length-1)
       
       
    'write string to a utf8 file
       FileWrite(c2,string)
       
       FileClose(c1)
       FileClose(c2)

    To export a file:

    Code:
    'convert from native utf-8 to extended ascii
       FileOpen(c1,"ascii file.txt",cRandom)
       FileOpen(c2,
    "utf8 file.txt",cRead)
       
       
    'set page code to 1252 or 28591
       tbin1.New2(c1,1252)

       
    'get string from utf8 file
       string=FileReadToEnd(c2)

       
    'convert string to bytes
       length=StrLength(string)
       
    Dim temp_array(length) As byte
       temp_array()=tbin1.StringToBytes(
    string)

       
    'write bytes to ascii file
       tbin1.WriteBytes(temp_array())
       
       FileClose(c1)
       FileClose(c2)

    use page code 1200 if you need to convert to/from utf16

    Enjoy!
     
  2. agraham

    agraham Expert Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice