Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Code Samples & Tips
Home Register FAQ Members List 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, 08:24 PM
Junior Member
 
Join Date: Sep 2007
Posts: 28
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, 05:06 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,470
Awards Showcase
Beta Tester Forum Contributer 
Total Awards: 2
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 ByteConverter library
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 On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 11:21 PM.


Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0