2015 AnsiToUtf8 和 Utf8ToAnsi
在服务端数据库的处理当中,涉及中文字符的结构体字段,需要转为Utf8后再存储到表项中。
从数据库中取出包含中文字符的字段后,如果需要保存到char *类型的结构体成员中,需要转为Ansi后再保存。
从数据库中取出类型数字的字段后,如果需要保存到int型的结构体成员中,需要调用atoi函数进行处理后再保存。
从数据库中取出包含中文字符的字段后,如果需要保存到char *类型的结构体成员中,需要转为Ansi后再保存。
从数据库中取出类型数字的字段后,如果需要保存到int型的结构体成员中,需要调用atoi函数进行处理后再保存。
1
static
char
*
AnsiToUtf8(
char
*
pchAnsi)
2 {
3 int uSize = 0 ;
4 WCHAR * pwText = NULL;
5 char * pchUtf8 = NULL;
6 int nUtf8Len = 0 ;
7
8 if (NULL == pchAnsi)
9 {
10 return NULL;
11 }
12
13 do
14 {
15 uSize = MultiByteToWideChar(CP_ACP, 0 ,pchAnsi, - 1 ,NULL, 0 );
16 if (uSize <= 0 )
17 {
18 break ;
19 }
20 pwText = (wchar_t * )malloc((uSize + 1 ) * sizeof (wchar_t));
21 if ( ! pwText)
22 {
23 break ;
24 }
25
26 memset(( void * )pwText, 0 , sizeof (wchar_t) * (uSize + 1 ));
27 MultiByteToWideChar(CP_ACP, 0 ,pchAnsi, - 1 ,pwText,uSize);
28
29 // widechar to utf8
30 nUtf8Len = WideCharToMultiByte(CP_UTF8, 0 , pwText, - 1 , NULL, 0 , NULL, NULL);
31 if ( 0 >= nUtf8Len)
32 {
33 break ;
34 }
35
36 pchUtf8 = ( char * )malloc(nUtf8Len + 2 );
37 if (NULL == pchUtf8)
38 {
39 break ;
40 }
41 memset(pchUtf8, 0 ,nUtf8Len + 2 );
42
43 nUtf8Len = WideCharToMultiByte(CP_UTF8, 0 , pwText, - 1 , pchUtf8, nUtf8Len, NULL, NULL);
44 if ( 0 >= nUtf8Len)
45 {
46 free(pchUtf8);
47 pchUtf8 = NULL;
48 break ;
49 }
50
51
52 } while ( 0 );
53
54 if (NULL != pwText)
55 {
56 free(pwText);
57 }
58
59 return pchUtf8;
60 }
61
62 static char * Utf8ToAnsi( char * pchUtf8)
63 {
64 int uSize = 0 ;
65 WCHAR * pwText = NULL;
66 char * pchAnsic = NULL;
67 int ansicStrLen = 0 ;
68
69 if (NULL == pchUtf8)
70 {
71 return NULL;
72 }
73
74 do
75 {
76 uSize = MultiByteToWideChar(CP_UTF8, 0 ,pchUtf8, - 1 ,NULL, 0 );
77 if (uSize <= 0 )
78 {
79 break ;
80 }
81 pwText = (wchar_t * )malloc((uSize + 1 ) * sizeof (wchar_t));
82 if ( ! pwText)
83 {
84 break ;
85 }
86 memset(( void * )pwText, 0 , sizeof (wchar_t) * (uSize + 1 ));
87 MultiByteToWideChar(CP_UTF8, 0 ,pchUtf8, - 1 ,pwText,uSize);
88
89 // widechar to utf8
90 ansicStrLen = WideCharToMultiByte(CP_ACP, 0 , pwText, - 1 , NULL, 0 , NULL, NULL);
91 if ( 0 >= ansicStrLen)
92 {
93 break ;
94 }
95
96 pchAnsic = ( char * )malloc(ansicStrLen + 2 );
97 if (NULL == pchAnsic)
98 {
99 break ;
100 }
101 memset(pchAnsic, 0 ,ansicStrLen + 2 );
102
103 ansicStrLen = WideCharToMultiByte(CP_ACP, 0 , pwText, - 1 , pchAnsic, ansicStrLen, NULL, NULL);
104 if ( 0 >= ansicStrLen)
105 {
106 free(pchAnsic);
107 pchAnsic = NULL;
108 break ;
109 }
110
111 } while ( 0 );
112
113 if (NULL != pwText)
114 {
115 free(pwText);
116 }
117
118 return pchAnsic;
119
120 }
2 {
3 int uSize = 0 ;
4 WCHAR * pwText = NULL;
5 char * pchUtf8 = NULL;
6 int nUtf8Len = 0 ;
7
8 if (NULL == pchAnsi)
9 {
10 return NULL;
11 }
12
13 do
14 {
15 uSize = MultiByteToWideChar(CP_ACP, 0 ,pchAnsi, - 1 ,NULL, 0 );
16 if (uSize <= 0 )
17 {
18 break ;
19 }
20 pwText = (wchar_t * )malloc((uSize + 1 ) * sizeof (wchar_t));
21 if ( ! pwText)
22 {
23 break ;
24 }
25
26 memset(( void * )pwText, 0 , sizeof (wchar_t) * (uSize + 1 ));
27 MultiByteToWideChar(CP_ACP, 0 ,pchAnsi, - 1 ,pwText,uSize);
28
29 // widechar to utf8
30 nUtf8Len = WideCharToMultiByte(CP_UTF8, 0 , pwText, - 1 , NULL, 0 , NULL, NULL);
31 if ( 0 >= nUtf8Len)
32 {
33 break ;
34 }
35
36 pchUtf8 = ( char * )malloc(nUtf8Len + 2 );
37 if (NULL == pchUtf8)
38 {
39 break ;
40 }
41 memset(pchUtf8, 0 ,nUtf8Len + 2 );
42
43 nUtf8Len = WideCharToMultiByte(CP_UTF8, 0 , pwText, - 1 , pchUtf8, nUtf8Len, NULL, NULL);
44 if ( 0 >= nUtf8Len)
45 {
46 free(pchUtf8);
47 pchUtf8 = NULL;
48 break ;
49 }
50
51
52 } while ( 0 );
53
54 if (NULL != pwText)
55 {
56 free(pwText);
57 }
58
59 return pchUtf8;
60 }
61
62 static char * Utf8ToAnsi( char * pchUtf8)
63 {
64 int uSize = 0 ;
65 WCHAR * pwText = NULL;
66 char * pchAnsic = NULL;
67 int ansicStrLen = 0 ;
68
69 if (NULL == pchUtf8)
70 {
71 return NULL;
72 }
73
74 do
75 {
76 uSize = MultiByteToWideChar(CP_UTF8, 0 ,pchUtf8, - 1 ,NULL, 0 );
77 if (uSize <= 0 )
78 {
79 break ;
80 }
81 pwText = (wchar_t * )malloc((uSize + 1 ) * sizeof (wchar_t));
82 if ( ! pwText)
83 {
84 break ;
85 }
86 memset(( void * )pwText, 0 , sizeof (wchar_t) * (uSize + 1 ));
87 MultiByteToWideChar(CP_UTF8, 0 ,pchUtf8, - 1 ,pwText,uSize);
88
89 // widechar to utf8
90 ansicStrLen = WideCharToMultiByte(CP_ACP, 0 , pwText, - 1 , NULL, 0 , NULL, NULL);
91 if ( 0 >= ansicStrLen)
92 {
93 break ;
94 }
95
96 pchAnsic = ( char * )malloc(ansicStrLen + 2 );
97 if (NULL == pchAnsic)
98 {
99 break ;
100 }
101 memset(pchAnsic, 0 ,ansicStrLen + 2 );
102
103 ansicStrLen = WideCharToMultiByte(CP_ACP, 0 , pwText, - 1 , pchAnsic, ansicStrLen, NULL, NULL);
104 if ( 0 >= ansicStrLen)
105 {
106 free(pchAnsic);
107 pchAnsic = NULL;
108 break ;
109 }
110
111 } while ( 0 );
112
113 if (NULL != pwText)
114 {
115 free(pwText);
116 }
117
118 return pchAnsic;
119
120 }