我的Powerbuilder版本是6.5,不能使用更高的版本,因为这是我所支持的。
我的问题是,当我执行dw_1.ImportFile(文件)时,第一行和第一列有一个有趣的字符串,如下所示:

直到我尝试打开该文件并将其保存到一个新的文本文件中,并试图导入新的file.which,这一点我才明白。
我的结论是,之所以会发生这种情况,是因为文件是UTF-8 (如NOTEPAD++所示),而新文件是Ansi。我试图导入的文件是由第三方自动给出的,我的用户不想要这样做的额外工作。
如何在powerbuilder中强制将此文件转换为ANSI。如果没有,我可能不得不做命令提示符转换,有什么想法吗?
发布于 2014-03-12 09:24:56
奇怪的
字符是(可选的) utf-8 BOM,它告诉编辑器文件是utf-8编码的(因为除非我们遇到代码127以上的转义字符,否则很难知道它)。您不能直接删除它,因为如果您的文件包含127个以上的字符(重音或任何特殊字符),则显示的数据中仍然会有垃圾(例如:é
-> é
,€
-> €
,.)其中的特殊字符将从2到4个垃圾字符。
我最近需要将一些utf-8编码字符串转换为"ansi“windows 1252编码。对于PB10+版本,utf-8和ansi之间的重新编码非常简单
b = blob(s, encodingutf8!)
s2 = string(b, encodingansi!)
但是string()
和blob()
不支持PB第10版之前的编码规范。
您可以做的是自己读取文件,跳过BOM,要求Windows通过MultiByteToWideChar()
+ WideCharToMultiByte()
转换字符串编码,并使用ImportString()
加载DW中转换的字符串。
获取文件内容的概念证明(使用此读取方法,文件不能大于2GB):
string ls_path, ls_file, ls_chunk, ls_ansi
ls_path = sle_path.text
int li_file
if not fileexists(ls_path) then return
li_file = FileOpen(ls_path, streammode!)
if li_file > 0 then
FileSeek(li_file, 3, FromBeginning!) //skip the utf-8 BOM
//read the file by blocks, FileRead is limited to 32kB
do while FileRead(li_file, ls_chunk) > 0
ls_file += ls_chunk //concatenate in loop works but is not so performant
loop
FileClose(li_file)
ls_ansi = utf8_to_ansi(ls_file)
dw_tab.importstring( text!, ls_ansi)
end if
utf8_to_ansi()
是一个全局函数,它是为PB9编写的,但是它应该与PB6.5一样工作:
global type utf8_to_ansi from function_object
end type
type prototypes
function ulong MultiByteToWideChar(ulong CodePage, ulong dwflags, ref string lpmultibytestr, ulong cchmultibyte, ref blob lpwidecharstr, ulong cchwidechar) library "kernel32.dll"
function ulong WideCharToMultiByte(ulong CodePage, ulong dwFlags, ref blob lpWideCharStr, ulong cchWideChar, ref string lpMultiByteStr, ulong cbMultiByte, ref string lpUsedDefaultChar, ref boolean lpUsedDefaultChar) library "kernel32.dll"
end prototypes
forward prototypes
global function string utf8_to_ansi (string as_utf8)
end prototypes
global function string utf8_to_ansi (string as_utf8);
//convert utf-8 -> ansi
//use a wide-char native string as pivot
constant ulong CP_ACP = 0
constant ulong CP_UTF8 = 65001
string ls_wide, ls_ansi, ls_null
blob lbl_wide
ulong ul_len
boolean lb_flag
setnull(ls_null)
lb_flag = false
//get utf-8 string length converted as wide-char
setnull(lbl_wide)
ul_len = multibytetowidechar(CP_UTF8, 0, as_utf8, -1, lbl_wide, 0)
//allocate buffer to let windows write into
ls_wide = space(ul_len * 2)
lbl_wide = blob(ls_wide)
//convert utf-8 -> wide char
ul_len = multibytetowidechar(CP_UTF8, 0, as_utf8, -1, lbl_wide, ul_len)
//get the final ansi string length
setnull(ls_ansi)
ul_len = widechartomultibyte(CP_ACP, 0, lbl_wide, -1, ls_ansi, 0, ls_null, lb_flag)
//allocate buffer to let windows write into
ls_ansi = space(ul_len)
//convert wide-char -> ansi
ul_len = widechartomultibyte(CP_ACP, 0, lbl_wide, -1, ls_ansi, ul_len, ls_null, lb_flag)
return ls_ansi
end function
https://stackoverflow.com/questions/22336948
复制相似问题