2016-09-26 45 views
0

我需要將文本文件從utf8轉換爲cp1251。我不能使用任何第三方軟件。有沒有用COBOL編寫的例程?它是Windows上的Micro Focus Cobol。用於文件編碼轉換的COBOL程序

+0

這是一個簡單的讀寫程序。然後查看Micro Focus網站上的各種文檔,瞭解如何將Unicode轉換爲代碼頁。 –

+0

「我不能使用任何第三方軟件?」然而,你正在提議編寫另一種工具,這種工具在我看來很像第三方軟件。 –

+0

雖然它可能被視爲第三方,但可以在文本編輯器(如TextPad或NotePad ++或Crimson Editor或...)中打開該文件,並保存爲其他編碼。如果你需要告訴管理員「它是用COBOL編寫的」,那麼「CALL」SYSTEM「USING」a-batchfile-invoking-a-scriptable-editor-with-this-filename「 –

回答

5

答:有很多COBOL程序的該書面...

我不知道任何免費的(=開源與自由的實際使用它)的實現,但你可以很容易地把它寫在你的擁有。 只需通過源代碼並將其移動到目標,如果cp1251中沒有可用的符號,則使用'?'管他呢。 這裏唯一的工作是:你需要查找x'80'以上的128個字符......

或者你檢查MF是否有一些特定的擴展名或者你自己寫的。 在SO上沒有「請爲我編碼」,所以你應該顯示你已經試過的東西。

爲了讓你的想法看看的this javascript sample轉換,應該是這樣的(未測試的代碼):

 77 utf-8-field  PIC X(5000). 
     77 new-char  PIC X. 
     77 cp1251-field PIC X(5000). 
     77 utf-8-pos  PIC 9(04) COMP-5. 
     77 cp1251-pos  PIC 9(04) COMP-5. 
     77 utf-8-end  PIC 9(04) COMP-5. 

     MOVE FUNCTION LENGTH (FUNCTION TRIM (utf-8-field TRAILING)) 
     TO utf-8-end 
     MOVE 1 TO cp1251-pos 
     PERFORM VARYING utf-8-pos FROM 1 BY 1 
       UNTIL utf-8-pos = utf-8-end 
      EVALUATE TRUE 
      *> normal ASCII character 
      WHEN utf-8-field (utf-8-pos) < x'80' 
       MOVE utf-8-field (utf-8-pos) TO new-char 
      *> UTF-8 in CP1251 range 
      WHEN utf-8-field (utf-8-pos) < x'04' 
       *> skip the first byte 
       ADD 1 TO utf-8-pos 
       EVALUATE TRUE 
        WHEN utf-8-pos > utf-8-end 
         MOVE '?' TO new-char 
        WHEN utf-8-field (utf-8-pos) = x'51' 
         MOVE x'B8' TO new-char 
        WHEN utf-8-field (utf-8-pos) >= x'4F' 
         MOVE '?' TO new-char 
        *> alternative: use alphabet conversion here 
        WHEN utf-8-field (utf-8-pos) = x'01' 
         MOVE x'A8' TO new-char 
        WHEN OTHER 
         MOVE utf-8-field (utf-8-pos) TO new-char 
         INSPECT new-char CONVERTING x'0203 ... 
             TO   x'B2B2 ... 
       END-EVALUATE 
      *> UTF-8 with no CP1251 char 
      *> Todo: check for other multibyte headers and add the correct 
      *>  number of characters to utf-8-pos 
      *> WHEN ... 
      WHEN OTHER 
       MOVE '?' TO new-char 
      END-EVALUATE 
      STRING new-char 
       DELIMITED BY SIZE 
       INTO cp1251-field 
       WITH POINTER cp1251-pos 
      END-STRING 
     END-PERFORM 

您可能希望定義一個ALPHABETCONVERTING x'0203 ... TO x'B2B3 ...部分:

 SPECIAL-NAMES. 
      ALPHABET UTF8-PART-2 IS x'01', x'02' THRU x'4F', x'51'. 
      ALPHABET CP1251  IS x'A8', x'B2' THRU x'FF', x'B8'. 

和內EVALUATE使用

  MOVE utf-8-field (utf-8-pos) TO new-char 
      INSPECT new-char CONVERTING UTF8-PART-2 TO CP1251