-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #ifndef CodeConver_H
- #define CodeConver_H
-
- namespace CodeConvert
- {
-
- #include <stdio.h>
- #include <string.h>
- #include <windows.h>
- #include <assert.h>
- #include <jni.h>
- #include <jni_md.h>
-
-
-
- wchar_t * ANSIToUnicode( const char* str )
- {
- int textlen ;
- wchar_t * result;
-
- textlen = MultiByteToWideChar( CP_ACP, 0, str,-1, NULL,0 );
- if (textlen ==0)
- {
- return NULL;
- }
- result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));
- memset(result,0,(textlen+1)*sizeof(wchar_t));
- MultiByteToWideChar(CP_ACP, 0,str,-1,(LPWSTR)result,textlen );
- return result;
- }
-
-
-
-
-
-
- char * UnicodeToANSI( const wchar_t* str )
- {
- char* result;
- int textlen;
- textlen = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
- if (textlen ==0)
- {
- return NULL;
- }
- result =(char *)malloc((textlen+1)*sizeof(char));
- memset( result, 0, sizeof(char) * ( textlen + 1 ) );
- WideCharToMultiByte( CP_ACP, 0, str, -1, result, textlen, NULL, NULL );
- return result;
- }
-
-
-
-
-
-
- int UTF8ToUnicode( wchar_t * &result,const char* utf8 )
- {
- int textlen ;
- textlen = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)utf8,-1, NULL,0 );
- if (textlen == 0)
- {
- return NULL;
- }
- result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));
- memset(result,0,textlen * 2 + 2);
- int widelen = MultiByteToWideChar(CP_UTF8, 0,(LPCSTR)utf8,-1,(LPWSTR)result,textlen );
- return widelen-1;
-
- }
-
-
-
-
-
-
- char * UnicodeToUTF8( const wchar_t* str )
- {
- char* result;
- int textlen;
- textlen = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
- if (textlen == 0)
- {
- return NULL;
- }
- result =(char *)malloc((textlen+1)*sizeof(char));
- memset(result, 0, sizeof(char) * ( textlen + 1 ) );
- WideCharToMultiByte( CP_UTF8, 0, str, -1, result, textlen, NULL, NULL );
- return result;
- }
-
-
-
-
-
- char* w2m(const wchar_t* wcs)
- {
- int len;
- char* buf;
- if (wcs =NULL)
- {
- return NULL;
- }
- len =wcstombs(NULL,wcs,0);
- if (len == 0)
- return NULL;
- buf = (char *)malloc(sizeof(char)*(len+1));
- memset(buf, 0, sizeof(char) *(len+1));
- len =wcstombs(buf,wcs,len+1);
- return buf;
- }
-
-
-
-
-
-
- wchar_t* multiByte_to_wideChar(const char* mbs)
- {
- int len;
- wchar_t* buf;
- if (mbs == NULL)
- {
- return NULL;
- }
- len =mbstowcs(NULL,mbs,0);
- if (len == 0)
- return NULL;
- buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
- memset(buf, 0, sizeof(wchar_t) *(len+1));
- len =mbstowcs(buf,mbs,len+1);
- return buf;
- }
-
-
-
-
-
-
- char* ANSIToUTF8(const char* str)
- {
- return UnicodeToUTF8(ANSIToUnicode(str));
- }
-
-
-
-
-
-
- char* UTF8ToANSI(const char* str)
- {
- wchar_t * temp;
- if (str == NULL)
- {
- return NULL;
- }
- UTF8ToUnicode(temp,str);
- return UnicodeToANSI(temp);
- }
-
-
-
-
-
- void UTF8ToGB2312( const char * utf8, char * &gb2312 )
- {
- int nLen = MultiByteToWideChar( CP_UTF8, 0, utf8, -1, NULL, 0 );
- if (nLen == 0)
- {
- gb2312 = NULL;
- return ;
- }
-
- USHORT* pwszGB2312 = new USHORT[ nLen + 1 ];
- RtlZeroMemory( pwszGB2312, nLen * 2 + 2 );
-
- MultiByteToWideChar( CP_UTF8, 0, utf8, -1, pwszGB2312, nLen );
-
- nLen = WideCharToMultiByte( CP_ACP, 0, pwszGB2312, -1, NULL, 0, NULL, NULL );
-
- CHAR* pszGB2312 = new CHAR[ nLen + 1 ];
- RtlZeroMemory( pszGB2312, nLen + 1 );
-
- WideCharToMultiByte( CP_ACP, 0, pwszGB2312, -1, pszGB2312, nLen, NULL, NULL );
-
- gb2312 = pszGB2312;
-
-
-
- }
-
-
-
- int GB2312ToUTF8( const char * gb2312,char * &utf8 )
- {
- int nLen1 = MultiByteToWideChar( CP_ACP, 0, gb2312, -1, NULL, 0 );
- if (nLen1 == 0)
- {
- return 0;
- }
-
- USHORT* pwszUTF8 = new USHORT[ nLen1 + 1 ];
- RtlZeroMemory( pwszUTF8, nLen1 * 2 + 2 );
-
- MultiByteToWideChar( CP_ACP, 0, gb2312, -1, pwszUTF8, nLen1 );
-
- int nLen = WideCharToMultiByte( CP_UTF8, 0, pwszUTF8, -1, NULL, 0, NULL, NULL );
-
- CHAR* pszUTF8 = new CHAR[ nLen + 1 ];
- RtlZeroMemory( pszUTF8, nLen + 1 );
-
- WideCharToMultiByte( CP_UTF8, 0, pwszUTF8, -1, pszUTF8, nLen, NULL, NULL );
-
- utf8 = pszUTF8;
-
- return nLen1-1;
-
-
-
- }
-
-
-
- int Gb2312ToUnicode(wchar_t * &OutUnicode,const char *gb2312)
- {
- char * utf8 = NULL;
- if (gb2312==NULL)
- {
- OutUnicode = NULL;
- return 0;
- }
- GB2312ToUTF8(gb2312,utf8);
- int len = UTF8ToUnicode(OutUnicode,utf8);
- return len;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- DWORD HexToString( const char * pHex,
- DWORD hexLen,
- char * pByteString)
- {
- unsigned long i;
-
- if (pHex==NULL)
- return 1;
-
- if(hexLen <= 0)
- return 2;
-
- for(i=0;i<hexLen;i++)
- {
- if(((pHex[i]&0xf0)>>4)>=0 && ((pHex[i]&0xf0)>>4)<=9)
- pByteString[2*i]=((pHex[i]&0xf0)>>4)+0x30;
- else if(((pHex[i]&0xf0)>>4)>=10 && ((pHex[i]&0xf0)>>4)<=16)
- pByteString[2*i]=((pHex[i]&0xf0)>>4)+0x37;
-
- if((pHex[i]&0x0f)>=0 && (pHex[i]&0x0f)<=9)
- pByteString[2*i+1]=(pHex[i]&0x0f)+0x30;
- else if((pHex[i]&0x0f)>=10 && (pHex[i]&0x0f)<=16)
- pByteString[2*i+1]=(pHex[i]&0x0f)+0x37;
- }
- return 0;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- DWORD StringToHex( const BYTE * pByteString,
- DWORD dwStrLen,
- BYTE * pbHex)
- {
- DWORD i;
- DWORD dwRet;
- unsigned char * pTempBuf=NULL;
-
- if (pByteString==NULL || pbHex==NULL)
- return 1;
-
- if(dwStrLen <= 0)
- return 2;
-
- if ((dwStrLen%2)!=0)
- {
- pTempBuf=(BYTE*)malloc(dwStrLen+2);
- pTempBuf[0]=0x30;
- memcpy(&pTempBuf[1],pByteString,sizeof(BYTE)*dwStrLen);
- dwRet=StringToHex(pTempBuf,dwStrLen+1,pbHex);
- free(pTempBuf);
- return dwRet;
- }
- else
- {
- for(i=0;i<dwStrLen;i++)
- {
- if(i%2==0)
- {
- if(pByteString[i]>='0' && pByteString[i]<='9')
- pbHex[i/2]=(pByteString[i]-0x30)<<4;
- else if(pByteString[i]>='a' && pByteString[i]<='f')
- pbHex[i/2]=(pByteString[i]-0x57)<<4;
- else if(pByteString[i]>='A' && pByteString[i]<='F')
- pbHex[i/2]=(pByteString[i]-0x37)<<4;
- else
- return 3;
- }
- else
- {
- if(pByteString[i]>='0' && pByteString[i]<='9')
- pbHex[i/2]|=pByteString[i]-0x30;
- else if(pByteString[i]>='a' && pByteString[i]<='f')
- pbHex[i/2]|=pByteString[i]-0x57;
- else if(pByteString[i]>='A' && pByteString[i]<='F')
- pbHex[i/2]|=pByteString[i]-0x37;
- else
- return 4;
- }
- }
- }
- return 0;
- }
-
-
-
-
-
-
-
-
- int hex_2_ascii(char *data, char *buffer, int len)
- {
- const char ascTable[17] = {"0123456789ABCDEF"};
- char *tmp_p = buffer;
- int i, pos;
- pos = 0;
-
- for(i = 0; i < len; i++)
- {
- tmp_p[pos++] = ascTable[data[i]>>4&0x0f];
- tmp_p[pos++] = ascTable[data[i] & 0x0f];
- }
- tmp_p[pos] = '\0';
- return pos;
- }
-
-
-
-
-
-
-
-
-
-
- int ascii_2_hex(char *O_data,char *N_data, int len)
- {
- int i,j,tmp_len;
- char tmpData;
- for(i = 0; i < len; i++)
- {
- if ((O_data[i] >= '0') && (O_data[i] <= '9'))
- {
- tmpData = O_data[i] - '0';
- }
- else if ((O_data[i] >= 'A') && (O_data[i] <= 'F'))
- {
- tmpData = O_data[i] - 0x37;
- }
- else if((O_data[i] >= 'a') && (O_data[i] <= 'f'))
- {
- tmpData = O_data[i] - 0x57;
- }
- else{
- return -1;
- }
- O_data[i] = tmpData;
- }
- for(tmp_len = 0,j = 0; j < i; j+=2)
- {
- N_data[tmp_len++] = (O_data[j]<<4) | O_data[j+1];
- }
- return tmp_len;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void Soft_swap(void *dst,const void *src,size_t count)
- {
- int i;
- unsigned char *t;
- unsigned char *q = (unsigned char *)dst;
- unsigned char *p = (unsigned char*) src;
- t = q;
- if(t == p)
- {
- q = (unsigned char *)malloc(count);
- assert(q);
- }
- for( i=0; (size_t) i<count; i++ ) q[count-1 - i] = p[i];
- if(t == p) {
- memcpy(p, q, count);
- free(q);
- }
-
- }
-
-
-
-
- VOID UTF82GB2312( const char * utf8, const char * gb2312 )
- {
- int nLen = MultiByteToWideChar( CP_UTF8, 0, utf8 , -1, NULL, 0 );
-
- USHORT* pwszGB2312 = new USHORT[ nLen + 1 ];
- RtlZeroMemory( pwszGB2312, nLen * 2 + 2 );
-
- MultiByteToWideChar( CP_UTF8, 0, utf8 , -1, pwszGB2312, nLen );
-
- nLen = WideCharToMultiByte( CP_ACP, 0, pwszGB2312, -1, NULL, 0, NULL, NULL );
-
- CHAR* pszGB2312 = new CHAR[ nLen + 1 ];
- RtlZeroMemory( pszGB2312, nLen + 1 );
-
- WideCharToMultiByte( CP_ACP, 0, pwszGB2312, -1, pszGB2312, nLen, NULL, NULL );
-
- gb2312 = pszGB2312;
-
- delete [] pszGB2312;
- delete [] pwszGB2312;
- }
-
-
- VOID GB23122UTF8( const char * gb2312, const char * utf8 )
- {
- int nLen = MultiByteToWideChar( CP_ACP, 0, gb2312 , -1, NULL, 0 );
-
- USHORT* pwszUTF8 = new USHORT[ nLen + 1 ];
- RtlZeroMemory( pwszUTF8, nLen * 2 + 2 );
-
- MultiByteToWideChar( CP_ACP, 0, gb2312 , -1, pwszUTF8, nLen );
-
- nLen = WideCharToMultiByte( CP_UTF8, 0, pwszUTF8, -1, NULL, 0, NULL, NULL );
-
- CHAR* pszUTF8 = new CHAR[ nLen + 1 ];
- RtlZeroMemory( pszUTF8, nLen + 1 );
-
- WideCharToMultiByte( CP_UTF8, 0, pwszUTF8, -1, pszUTF8, nLen, NULL, NULL );
-
- utf8 = pszUTF8;
-
- delete [] pszUTF8;
- delete [] pwszUTF8;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- char* jstringToWindows( JNIEnv *env, jstring jstr )
- {
- int length = (env)->GetStringLength(jstr );
- const jchar* jcstr = (env)->GetStringChars(jstr, 0 );
- char* rtn = (char*)malloc( length*2+1 );
- int size = 0;
- size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL );
- if( size <= 0 )
- return NULL;
- (env)->ReleaseStringChars(jstr, jcstr );
- rtn[size] = 0;
- return rtn;
- }
-
-
-
-
- jstring WindowsTojstring( JNIEnv* env,const char* str )
- {
- jstring rtn = 0;
- int slen = strlen(str);
- unsigned short * buffer = 0;
- if( slen == 0 )
- rtn = (env)->NewStringUTF(str );
- else
- {
- int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );
- buffer = (unsigned short *)malloc( length*2 + 1 );
- if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length ) >0 )
- {
- rtn = (env)->NewString( (jchar*)buffer, length );
- }
-
- }
- if( buffer )
- free( buffer );
-
- return rtn;
- }
-
-
-
-
-
- jstring nativeTojstring( JNIEnv* env,const char* str )
- {
- jclass strClass = env->FindClass( "Ljava/lang/String;");
- jmethodID ctorID = env->GetMethodID( strClass, "<init>",
- "([BLjava/lang/String;)V");
-
- if (env->ExceptionCheck() == JNI_TRUE || str == NULL)
- {
- env->ExceptionDescribe();
- env->ExceptionClear();
- printf("nativeTojstring函数转换时,str为空/n");
- return NULL;
- }
-
- jbyteArray bytes = env->NewByteArray( strlen(str));
-
-
- env->SetByteArrayRegion( bytes, 0, strlen(str), (jbyte*)str);
- jstring encoding = env->NewStringUTF( "GBK");
- jstring strRtn = (jstring)env->NewObject( strClass, ctorID, bytes,
- encoding);
-
-
- return strRtn;
- }
-
-
-
-
- char* jstringToNative(JNIEnv *env, jstring jstr)
- {
- if (env->ExceptionCheck() == JNI_TRUE || jstr == NULL)
- {
- env->ExceptionDescribe();
- env->ExceptionClear();
- printf("jstringToNative函数转换时,传入的参数str为空");
- return NULL;
- }
-
- jbyteArray bytes = 0;
- jthrowable exc;
- char *result = 0;
- if (env->EnsureLocalCapacity(2) < 0) {
- return 0;
- }
- jclass jcls_str = env->FindClass("java/lang/String");
- jmethodID MID_String_getBytes = env->GetMethodID(jcls_str, "getBytes", "()[B");
-
- bytes = (jbyteArray)env->CallObjectMethod(jstr, MID_String_getBytes);
- exc = env->ExceptionOccurred();
- if (!exc) {
- jint len = env->GetArrayLength( bytes);
- result = (char *)malloc(len + 1);
- if (result == 0) {
-
-
-
- env->DeleteLocalRef(bytes);
- return 0;
- }
- env->GetByteArrayRegion(bytes, 0, len, (jbyte *)result);
- result[len] = 0;
- } else {
- env->DeleteLocalRef(exc);
- }
- env->DeleteLocalRef(bytes);
- return (char*)result;
- }
-
- }
-
-
- #endif
|