PowerBuilder 12如何确定输入文件的编码

时间:2012-01-29 16:03:13

标签: encoding powerbuilder

我是PowerBuilder 12的新手,想知道有没有办法确定输入文件的编码(例如Unicode,BIG5)。任何评论和代码示例表示赞赏!谢谢!

2 个答案:

答案 0 :(得分:1)

从PB 12.5帮助文件:

FileEncoding(filename)

filename:要为编码类型

测试的文件的名称

返回值 EncodingANSI! EncodingUTF8! EncodingUTF16LE! EncodingUTF16BE! 如果filename不存在,则返回null。

答案 1 :(得分:0)

如果您假设Unicode文件具有BOM前缀(但事实并非所有Unicode文件都具有此前缀),那么查找Unicode非常简单。下面是一些代码。但是,我不知道Big5;它看起来对我(乍一看规格,从来没有机会使用它),就像它没有类似的前缀。

祝你好运,

特里

function of_filetype (string as_filename) returns encoding

integer li_NullCount, li_NonNullCount, li_OffsetTest
long ll_File
encoding le_Return
blob lblb_UTF16BE, lblb_UTF16LE, lblb_UTF8, lblb_Test, lblb_BOMTest, lblb_Null

lblb_UTF16BE = Blob ("~hFE~hFF", EncodingANSI!)
lblb_UTF16LE = Blob ("~hFF~hFE", EncodingANSI!)
lblb_UTF8 = Blob ("~hEF~hBB~hBF", EncodingANSI!)
lblb_Null = blobmid (blob ("~h01", encodingutf16le!), 2, 1)

SetNull (le_Return)

// Get a set of bytes to test
ll_File = FileOpen (as_FileName, StreamMode!, Read!, Shared!)
FileRead (ll_File, lblb_Test)
FileClose (ll_File)

// test for BOMs: UTF-16BE (FF FE), UTF-16LE (FF FE), UTF-8 (EF BB BF)
lblb_BOMTest = BlobMid (lblb_Test, 1, Len (lblb_UTF16BE))
IF lblb_BOMTest = lblb_UTF16BE THEN RETURN EncodingUTF16BE!

lblb_BOMTest = BlobMid (lblb_Test, 1, Len (lblb_UTF16LE))
IF lblb_BOMTest = lblb_UTF16LE THEN RETURN EncodingUTF16LE!

lblb_BOMTest = BlobMid (lblb_Test, 1, Len (lblb_UTF8))
IF lblb_BOMTest = lblb_UTF8 THEN RETURN EncodingUTF8!

//I've removed a hack from here that I wouldn't encourage others to use, basically checking for 
//0x00 in places I'd "expect" them to be if it was a Unicode file, but that makes assumptions

RETURN le_Return