2014-11-05 45 views
0

我有一個從MQ讀取消息的程序。字符集是1047.因爲我的Java版本很舊,所以它不支持字符集。在Java中是否可以將字符集1047轉換爲另一個字符集,比如500?

在接收之後但在閱讀之前,是否可以在程序中將此字符串更改爲字符集500。

對於例如:

public void fun (String str){   //str in char set 1047. **1047 is not supported in my system** 
    /* can I convert str into char set 500 here. Convert it into byte stream and then back to  string. Something like this */ 

      byte [] b=str.getBytes(); 
      ByteArrayOutputStream baos = null; 
     try{ 
     baos = new ByteArrayOutputStream(); 
     baos.write(b); 
     String str = baos.toString("IBM-500"); 
     System.out.println(str); 
    } 

回答

0

字節[] B = str.getBytes(); //將使用file.encoding將字符串(編碼只能是jvm中的Unicode)轉換爲字節。你應該檢查str是否包含正確的信息,如果是這樣,你不需要關心1047編碼,只需運行str.getBytes(「IBM-500」),你將得到500個編碼字節。同樣,String對象只使用Unicode,如果將字符串轉換爲字節,則編碼對結果字節數組至關重要。

相關問題