• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1803
  • Last Modified:

Handling UTF8 Unix flat file, while maintaining international text characters

I am trying to write a series of VBA macros to parse and validate flat files, ready for import into a database.  The database, file format and technology are not changeable.

The flat file is sent to us, containing data that should be added, as a Unix text file, with UTF-8 encoding and no Byte Object Marker.  However, it can contain any and all number of foreign characters (Chinese, Taiwanese, Japanese, Sri Lankan, Arabic, etc).  

In its current state, my Excel application can read data, validate and recompile the desired output to another sheet, ready for export, without affecting the characters.  

The primary problem occurs when I try to export the file.  The exported file format has to match that of the imported file.  When I view the original file in SC Unipad, I can see the file is a Unix UTF-8 file, with no Byte Order Marker.  The output file, however, is a Unix UTF-16 Little Endian file, with a Byte Object Marker.

I am using the File System Object; creating the file with
    Set FileObj = FSO.CreateTextFile(OutputFile, Overwrite, True)

and writing with
    FileObj.Write (Sheets(OutSheet).Cells(RowCount, 1).Value)
    FileObj.Write EndLine                                                        ' Endline = Chr(10)

to write each individual row.

I have tried using the WideCharToMultiByte() external function (with Codepage = 65001) in a UTF8Encode() function, as suggested in previous questions, so the write line is
    FileObj.Write (UTF8Encode (Sheets(OutSheet).Cells(RowCount, 1).Value))

but this still creates a UTF-16 file and the foreign characters are lost in favour of ANSI(?) counterparts.

I figure the UTF-16 output might be due to the Unicode flag in FSO.CreateTextFile() being set to True, but I cannot see any other way of maintaining the characters.

I would prefer to avoid opening SC Unipad in a shell and using it to open the UTF-16 file, remove the BOM and convert to UTF-8, as it would rely too heavily on third party software.

Any help anyone can give would be greatly appreciated...
0
jimbobmcgee
Asked:
jimbobmcgee
  • 5
  • 3
1 Solution
 
jimbobmcgeeAuthor Commented:
Anybody?

...

:)
0
 
jimbobmcgeeAuthor Commented:
Please?

...

:)
0
 
Arundel_CastleCommented:
Try this link Jim Bob. :)

http://www.cyberactivex.com/UnicodeTutorialVb.htm

By the way my name is also Magee. Magee and McGee are the same name.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Arundel_CastleCommented:
Thanks for the points Jim, by the way my first name is also James:) Though I have no bob.
0
 
Arundel_CastleCommented:
Actually thats a pretty interesting question in working with multinational encoding from a unix based system.
0
 
Arundel_CastleCommented:
You don't have to give me the extra points for this question Jim. But it would be nice if you did. I would reach my experts total. I haven't used this site in a couple of years but I was an expert before.
0
 
jimbobmcgeeAuthor Commented:
>> Actually thats a pretty interesting question in working with multinational encoding from a unix based system.

It was a pain!!  I think my original problem was due to opening the text stream in unicode (utf16).  The second problem I had was due to my UTF8Encode function using WideCharToMultiByte and then returning a Left(...) on the result.  I think this converted the string back to UTF16.

Either way, all cleared up by the link you posted...
0
 
Arundel_CastleCommented:
Thanks for the points Jim, by the way it does sound like a nightmare to write for. Thanks again.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now