【问题标题】:Powerbuilder: Converting UTF-8 to UTF-8 without bomPowerbuilder:在没有 bom 的情况下将 UTF-8 转换为 UTF-8
【发布时间】:2020-07-29 04:02:07
【问题描述】:

晚安,我需要将文本文件从 UTF-8 转换为 UTF-8,而无需在 powerbuilder 中使用 bom 我可以识别并应该从文本中删除 bom,但是当我重新录制它时会继续使用 bom,请帮助

                integer li_fnum
                long ll_bytes
                
                li_fnum = FileOpen(ls_archivo, StreamMode!)
                ll_bytes = FileReadEx(li_fnum, lblb_file_contents)

                if len(lblb_file_contents) >= 3 then
                        GetByte(lblb_file_contents, 1, lbt_1)
                        GetByte(lblb_file_contents, 2, lbt_2)
                        GetByte(lblb_file_contents, 3, lbt_3)
                    if lbt_1 = 239 and lbt_2 = 187 and lbt_3 = 191 then // BOM for UTF8 = EF    BB BF
                        // BOM is found - remove it from the blob
                        lblb_file_contents = BlobMid(lblb_file_contents, 4, len(lblb_file_contents) - 3)
                        // Check the truncated contents once again
                        if len(lblb_file_contents) = 0 then
                        MessageBox("Error", "There's no data in the file!", StopSign!)
                        return
                        end if
                    end if
                end if

【问题讨论】:

  • 您可以删除长度参数,没有它,它将返回偏移量为 4 的 blob,例如BlobMid(lbl_file_contents, 4) 顺便说一句,这可能会有所帮助> stackoverflow.com/questions/22336948/…
  • 经过多次尝试,还是去掉了编码,还是非常感谢

标签: utf-8 powerbuilder


【解决方案1】:

您将数据类型 text 转换为 blob 并使用编码 UTF-8 并写下文件:

i=FileWriteEx(f, blob(text,EncodingUTF8!) )

【讨论】:

    【解决方案2】:
    Create function remove_utf8_bom (String as_file)
    
     Integer    li_fnum    
     Long       ll_bytes    
     String     ls_file_contents    
     Blob       lblb_file_contents     
     Byte       lbt_1, lbt_2, lbt_3
     
     li_fnum = FileOpen(as_file, StreamMode!)
    
     ll_bytes = FileReadEx(li_fnum, lblb_file_contents)
    
     FileClose(li_fnum)
    
     if len(lblb_file_contents) >= 3 then
    
                GetByte(lblb_file_contents, 1, lbt_1)    
                GetByte(lblb_file_contents, 2, lbt_2)    
                GetByte(lblb_file_contents, 3, lbt_3)
    
          if lbt_1 = 239 and lbt_2 = 187 and lbt_3 = 191 then // BOM for UTF8 = EF    BB BF
                // BOM is found - remove it from the blob
                lblb_file_contents = BlobMid(lblb_file_contents, 4, len(lblb_file_contents) - 3)
    
                // Check the truncated contents once again
                if len(lblb_file_contents) = 0 then    
                    //MessageBox("Error", "There's no data in the file!", StopSign!)
                    return    
                end if
    
          end if
    
     end if
     
     li_fnum = FileOpen(as_file, StreamMode!, Write!, Shared!, Replace!)    
     FileWriteEx(li_fnum, blob(string(lblb_file_contents, EncodingUTF8!),EncodingUTF8!) )    
     FileClose(li_fnum)
     
    

    【讨论】:

    • 欢迎来到 Stack Overflow!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
    猜你喜欢
    • 2013-11-15
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多