Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Code Samples & Tips
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Code Samples & Tips Share your recent discoveries and ideas with other users.

Coverting text files with binary file page code functionality

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-20-2008, 09:24 PM
Knows the basics
 
Join Date: Sep 2007
Posts: 51
Default Coverting text files with binary file page code functionality

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!
Reply With Quote
  #2 (permalink)  
Old 06-21-2008, 06:06 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

This post has inspired me to add a CodepageFile object to my BytesConverter library so you can read/write using codepages without having to go through a byte array. See http://www.basic4ppc.com/forum/addit...r-library.html
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Add file association to Basic4ppc source code files (on the device) Erel Code Samples & Tips 22 07-01-2010 08:06 PM
Reading binary files jgm Questions (Windows Mobile) 5 05-28-2008 11:09 AM
Binary files on device Ianmac Questions (Windows Mobile) 4 08-24-2007 08:49 AM
Binary Files- Love'm and sometimes confused XerVision Questions (Windows Mobile) 2 06-10-2007 08:55 PM
Dividing the code into text files... Cableguy Questions (Windows Mobile) 5 05-21-2007 04:21 PM


All times are GMT. The time now is 05:32 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0