iconv 轉換字元編碼,相容VC轉換

2021-06-06 13:15:35 字數 3800 閱讀 7968

#ifndef __char_convert_h__

#define __char_convert_h__

#ifdef os_windows

#include //"targetver.h"

#ifndef win32_lean_and_mean

#define win32_lean_and_mean // 從 windows 標頭檔案中排除極少使用的資訊

#endif

// windows 標頭檔案:

#include #else // linux

#include "iconv.h"

#ifndef iconv_buffer_size

#define iconv_buffer_size 1024

#endif

#define wchar wchar_t

#ifdef _unicode

#define tchar wchar_t

#else

#define tchar char

#endif

#endif

/#define e_char cwchartochar::echar

#define e_wchar cwchartochar::ewchar

#define e_utf8 cwchartochar::eutf8

#define null_str cwchartochar::getnullstr()

static char g_null[2] = ;

/// 簡單的wchar_t 和 char 轉換類, 且包含與utf8的轉換

class cwchartochar

;private:

char * m_cdest;

wchar_t * m_wcdest;

char * m_cutf8;

unsigned int m_nsrctype;

public:

char * char(void)

wchar_t * wchar(void)

bool isnullchar(void)

bool isnullwchar(void)

#if defined( _utf8_ )

char * utf8(void)

bool isnullutf8(void)

#endif

tchar * tchar(void)

// ansi/gbk 轉 unicode和utf8的建構函式

cwchartochar(const char* psrc, unsigned int nsrctype = echar

#if defined( _utf8_ )

, unsigned int ndesttype = eutf8)

#else

, unsigned int ndesttype = ewchar)

#endif

: m_cdest(null)

, m_wcdest(null)

, m_cutf8(null)

, m_nsrctype(nsrctype)

#ifdef os_windows

if( echar == m_nsrctype )

else if( eutf8 == m_nsrctype )

#else // linux

if( echar == m_nsrctype )

else if( eutf8 == m_nsrctype )

#endif

}// unicode 轉 ansi/gbk和utf8的建構函式

cwchartochar(const wchar_t * psrc

#if defined( _utf8_ )

, unsigned int ndesttype = eutf8)

#else

, unsigned int ndesttype = echar)

#endif

: m_cdest(null)

, m_wcdest(null)

, m_cutf8(null)

, m_nsrctype(ewchar)

#ifdef os_windows

// ansi/gbk

#if defined( _utf8_ )

#endif

#else // linux

if( ndesttype & echar)

do_iconv_convert((char*)psrc, &m_cdest, "wchar_t", "gbk");

if( ndesttype & eutf8)

do_iconv_convert((char*)psrc, (char**)&m_cutf8, "wchar_t", "utf-8");

#endif

}~cwchartochar()

else if( ewchar == m_nsrctype )

else if( eutf8 == m_nsrctype )

}// using iconv for linux

#ifndef os_windows

int do_iconv_convert(const char* psrc, char**ppout, const char* pszfromcode, const char* psztocode)

iconv(cd, null, null, null, null);

size_t nslen = 0, nolen = 0;

if( m_nsrctype == ewchar )

#ifdef os_windows

nslen = wcslen((wchar_t*)psrc) * 2; // 乘以2,轉換成char的長度

#else // linux

nslen = wcslen((wchar_t*)psrc) * 4; // for unicode -> gbk/utf8, linux 乘以4

#endif

else

nslen = strlen(psrc);

if( 0 == strcmp( psztocode, "wchar_t"))

nolen = nslen * 4; // for gbk -> unicode

else

nolen = nslen * 2;

size_t norawlen = nolen;

char* pout = new char[nolen+4];

char* poutstart = pout;

char ** ppin = (char**)&psrc;

char ** ppout = &pout;

#ifdef _debug

printf("convert: %s[%lu] -> %s[%lu] bytes\n", pszfromcode, nslen, psztocode, nolen);

#endif

size_t ret = iconv(cd, (char**)ppin, &nslen, (char**)ppout, &nolen);

if( 0 == ret ) // 轉換完成成功

else // 全部或部分錯誤

}#endif

static const char* getnullstr(void)

};#endif //__char_convert_h__

linux c 字元編碼轉換函式 iconv

一 利用iconv函式族進行編碼轉換 在linux上進行編碼轉換時,既可以利用iconv函式族程式設計實現,也可以利用iconv命令來實現,只不過後者是針對檔案的,即將指定檔案從一種編碼轉換為另一種編碼。iconv函式族的標頭檔案是iconv.h,使用前需包含之。include iconv函式族有三...

iconv 檔案編碼轉換

linux shell 配置檔案中預設的字符集編碼為utf 8 utf 8是unicode的一種表達方式,gb2312是和unicode都是字元的編碼方式,所以說gb2312跟utf 8的概念應該不是乙個層次上的。在linux上進行編碼轉換時,可以利用iconv命令實現,這是針對檔案的,即將指定檔案...

iconv 檔案編碼轉換

linux shell 配置檔案中預設的字符集編碼為utf 8 utf 8是unicode的一種表達方式,gb2312是和unicode都是字元的編碼方式,所以說gb2312跟utf 8的概念應該不是乙個層次上的。在linux上進行編碼轉換時,可以利用iconv命令實現,這是針對檔案的,即將指定檔案...