歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux C++ 字符串 編碼識別

Linux C++ 字符串 編碼識別

日期:2017/3/1 11:09:20   编辑:Linux編程

最近在用Qt做一個ftp客戶端(其實qt的demo例子中就有一個簡單的ftp客戶端軟件,只是功能太簡單),由於客戶端需要通用的話,要跟不同的ftp服務器相連,不同的服務器的編碼格式可能不同,而在客戶端要不中文亂碼(英文不論是什麼編碼都可以正常顯示的啦),就涉及到編碼識別的問題。

下載 libchardet這個庫,使用說明就算了,直接讀頭文件吧。

libchardet-0.0.4.tar.gz下載地址:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/2011/10/29/Linux C++ 字符串 編碼識別/

下面是一個簡單的示例,

  1. #include "chardet.h"
  2. char out_encode[CHARDET_MAX_ENCODING_NAME]
  3. char * EncodeUtil::GetLocalEncoding(const char* in_str, unsigned int str_len, char* out_encode){
  4. chardet_t chardect=NULL;
  5. if(chardet_create(&chardect)==CHARDET_RESULT_OK){
  6. if(chardet_handle_data(chardect, in_str, (unsigned int)str_len) == CHARDET_RESULT_OK)
  7. if(chardet_data_end(chardect) == CHARDET_RESULT_OK)
  8. chardet_get_charset(chardect, out_encode, CHARDET_MAX_ENCODING_NAME);
  9. }
  10. if(chardect)
  11. chardet_destroy(chardect);
  12. return out_encode;
  13. }

更多參考見 http://www.linuxidc.com/Linux/2011-10/46184p2.htm

Copyright © Linux教程網 All Rights Reserved