日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

字符编码转换_进制转换(GB2312,GBK,JNI,HexTOStr)

發布時間:2025/3/12 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字符编码转换_进制转换(GB2312,GBK,JNI,HexTOStr) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[cpp] view plain copy print?
  • //??
  • /*??
  • ASCII?英文一個字節???
  • gb2312,gbk?中文兩個字節,英文一個字節???
  • 在中文系統中ansi一般指gb2312或gbk???
  • GB2312、GBK都屬于雙字節字符集?(DBCS)???
  • Utf-8?中文三個字節,英文一個字節???
  • Unicode?中文兩個字節,英文兩個字?
  • */??
  • //??
  • //??
  • /*?
  • GB2312?中國國家?標準碼?
  • ?
  • ????ANSI碼(American?National?Standards?Institute)?
  • ????美國國家??標準學會?的?標準碼?
  • ?
  • ????ASCII碼(America?Standard?Code?for?Information?Interchange)美國信息交換標準碼?
  • ?
  • ????你可以認為是不同的東西!?
  • ????ANSI碼僅在前126個與ASCII碼相同?
  • */??
  • //??
  • ??
  • //??
  • /*?
  • /*?
  • *???二進制、八進制、十進制、十六進制轉換???16進制和字符相互轉換??????????????????????????????????????????????????????????????????????????????????????????????????????
  • */??
  • //??
  • ??
  • //??
  • /*?
  • ????java內部是使用16bit的unicode編碼(UTF-16)來表示字符串的,無論中文英文都是2字節;?
  • ????jni內部是使用UTF-8編碼來表示字符串的,UTF-8是變長編碼的unicode,一般ascii字符是1字節,中文是3字節;?
  • ????c/c++使用的是原始數據,ascii就是一個字節了,中文一般是GB2312編碼,用兩個字節來表示一個漢字。?
  • */??
  • //??
  • ??
  • //??
  • //???CodeConver.h??
  • //??
  • ??
  • #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>??
  • ??
  • ????//???ANSI?To?Unicode??
  • ????//???測試通過??
  • ????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;???
  • ????}??
  • ??
  • ????/************************************************************************/??
  • ????//??Unicode??To?ANSI(美國國家標準碼)??
  • ????//??測試通過??
  • ????//??[2/8/2012?liu]??
  • ????/************************************************************************/??
  • ????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;??
  • ????}??
  • ??
  • ????/************************************************************************/??
  • ????//??UTF8?To?Unicode??
  • ????//??[2/8/2012?liu]??
  • ????//??測試通過??
  • ????/************************************************************************/??
  • ????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;??
  • ??
  • ????}??
  • ??
  • ????/************************************************************************/??
  • ????//??UnicodeToUTF8??
  • ????//????????????
  • ????//??[2/8/2012?liu]??
  • ????/************************************************************************/??
  • ????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;??
  • ????}??
  • ????/************************************************************************/??
  • ????//??寬字符?轉換為?多字符??Unicode?-?ANSI??
  • ????//????????????
  • ????//??[2/8/2012?liu]??
  • ????/************************************************************************/??
  • ????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;??
  • ????}??
  • ??
  • ????/************************************************************************/??
  • ????//??多字符?轉換為?寬字符??ANSI?-?Unicode??
  • ????//???
  • ????//??[2/8/2012?liu]??
  • ????/************************************************************************/??
  • ????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;??
  • ????}??
  • ??
  • ????/************************************************************************/??
  • ????//??ANSI?To?UTF8??
  • ????//??????????
  • ????//??[2/8/2012?liu]??
  • ????/************************************************************************/??
  • ????char*?ANSIToUTF8(const?char*?str)??
  • ????{??
  • ????????return?UnicodeToUTF8(ANSIToUnicode(str));??
  • ????}??
  • ??
  • ????/************************************************************************/??
  • ????//??UTF8?To?ANSI??
  • ????//??測試通過??
  • ????//??[2/8/2012?liu]??
  • ????/************************************************************************/??
  • ????char*?UTF8ToANSI(const?char*?str)??
  • ????{??
  • ????????wchar_t?*?temp;??
  • ????????if?(str?==?NULL)??
  • ????????{??
  • ????????????return?NULL;??
  • ????????}??
  • ????????UTF8ToUnicode(temp,str);??
  • ????????return?UnicodeToANSI(temp);??
  • ????}??
  • ??
  • ??????
  • ??
  • ????//??測試OK??
  • ????//??UTF8??convert?to?GB2312??
  • ????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;??
  • ??????????
  • ????//??????delete?[]?pszGB2312;??
  • ????//??????delete?[]?pwszGB2312;??
  • ????}??
  • ??
  • ????//?GB2312?convert?to?UTF8??
  • ????//??測試通過??
  • ????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;??
  • ??
  • ????//??delete?[]?pszUTF8;??
  • ????//??delete?[]?pwszUTF8;??
  • ????}??
  • ??
  • ????//??Gb2312?To?Unicode??
  • ????//??測試通過??
  • ????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;??
  • ????}??
  • ??????
  • //??
  • ??
  • ??????
  • ??
  • //??
  • ??
  • ?????/**************************************************************************?
  • ?????*????????????????????????????????????????????????????????????????????????
  • ?????*??函數名:??HexToString???????????????????????????????????
  • ?????*?????????????????????????????????????????????????????????????????????????
  • ?????*??描??述:??16進制?轉為?字符串???????????????????????????????
  • ?????*?????????????????????????????????????????????????????????????????????????
  • ?????*??參??數:??IN:const?char?*pHex,IN:unsigned?long?hexLen,?
  • ?????*???????????OUT:char?*pByteString????????????
  • ?????*?????????????????????????????????????????????????????????????????????????
  • ?????*??返??回:??成功:0?,???失敗:1、2??????????????????????????????????????????
  • ?????*???
  • ?????*??作??者:?liuguanglin???????
  • ?????*?
  • ????**************************************************************************/??
  • ????DWORD?HexToString(/*IN*/??const?char????*?pHex,??
  • ??????????????????????/*IN*/??DWORD???????????hexLen,??
  • ??????????????????????/*OUT*/?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;???//??小寫:0x37?改為?0x57???
  • ??????????????
  • ????????????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;??????//??小寫:0x37?改為?0x57???
  • ????????}??
  • ????????return?0;??
  • ????}??
  • ??
  • ????/**************************************************************************?
  • ?????*????????????????????????????????????????????????????????????????????????
  • ?????*??函數名:??StringToHex???????????????????????????????????
  • ?????*?????????????????????????????????????????????????????????????????????????
  • ?????*??描??述:??字符串?轉為?16進制???????????????????????????????
  • ?????*?????????????????????????????????????????????????????????????????????????
  • ?????*??參??數:??IN:const?char?*pHexBYTE,IN:DWORD?dwStrLen,OUT:BYTE?*pbHex????????????
  • ?????*?????????????????????????????????????????????????????????????????????????
  • ?????*??返??回:??成功:0?,???失敗:1、2??????????????????????????????????????????
  • ?????*????
  • ?????*??作??者:?liuguanglin?
  • ?????*?
  • ????**************************************************************************/??
  • ????DWORD?StringToHex(/*IN*/??const?BYTE?*?pByteString,??
  • ??????????????????????/*IN?*/?DWORD????????dwStrLen,??
  • ??????????????????????/*OUT*/?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;??
  • ????}??
  • ??
  • ????/*****************************************?
  • ????HEX?到?ASCII?的轉換函數?
  • ????入口參數:?data:?轉換數據的入口指針?
  • ????buffer:?轉換后數據入口指針?
  • ????len?:?需要轉換的長度?
  • ????返回參數:轉換后數據長度?
  • ????*******************************************/??
  • ????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;??
  • ????}??
  • ??
  • ????/*****************************************?
  • ????ASCII?到?HEX?的轉換函數?
  • ????入口參數:?O_data:?轉換數據的入口指針,?
  • ????N_data:?轉換后新數據的入口指針?
  • ????len?:?需要轉換的長度?
  • ????返回參數:-1:?轉換失敗?
  • ????其它:轉換后數據長度?
  • ????注意:O_data[]數組中的數據在轉換過程中會被修改。?
  • ????****************************************/??
  • ????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'))?//A....F??
  • ????????????{??
  • ????????????????tmpData?=?O_data[i]?-?0x37;??
  • ????????????}??
  • ????????????else?if((O_data[i]?>=?'a')?&&?(O_data[i]?<=?'f'))?//a....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;??
  • ????}??
  • ??
  • ??
  • ??
  • ????/**************************************************************************?
  • ?????*????????????????????????????????????????????????????????????????????????
  • ?????*??函數名:??Soft_swap???????????????????????????????????
  • ?????*?????????????????????????????????????????????????????????????????????????
  • ?????*??描??述:??RSA密鑰?倒序??????????????????????????????
  • ?????*?????????????????????????????????????????????????????????????????????????
  • ?????*??參??數:??IN:?void?*dst,?IN:?const?void?*src,?IN:?size_t?count????????????
  • ?????*?????????????????????????????????????????????????????????????????????????
  • ?????*??返??回:??無???????????????????????????????????????
  • ?????*????
  • ?????*??作??者:?liuguanglin?
  • ????**************************************************************************/??
  • ????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);??
  • ????????}??
  • ??
  • ????}??
  • ???
  • ????//??編碼轉換????
  • ??
  • ?????UTF8?convert?to?GB2312??
  • ????VOID?UTF82GB2312(?/*IN*/?const?char?*?utf8,?/*?OUT?*/?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;??
  • ????}??
  • ??
  • ?????GB2312?convert?to?UTF8??
  • ????VOID?GB23122UTF8(?/*IN*/?const?char?*?gb2312,?/*?OUT?*/?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;??
  • ????}???
  • ??
  • ??
  • ??
  • ??????
  • //-----------------------------------------------??JNI?部分??---------------------------------------??
  • /*?
  • ????java內部是使用16bit的unicode編碼(UTF-16)來表示字符串的,無論中文英文都是2字節;?
  • ????jni內部是使用UTF-8編碼來表示字符串的,UTF-8是變長編碼的unicode,一般ascii字符是1字節,中文是3字節;?
  • ????c/c++使用的是原始數據,ascii就是一個字節了,中文一般是GB2312編碼,用兩個字節來表示一個漢字。?
  • */??
  • ??????
  • ??
  • ??
  • ??
  • ????//??測試Ok??
  • ????//?只適用于?window平臺??
  • ????char*?jstringToWindows(?JNIEnv?*env,?jstring?jstr?)??
  • ????{?//?unicode(UTF-16)?轉換成?GB2312??
  • ????????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;??
  • ????}??
  • ??
  • ??
  • ????//??測試Ok??
  • ????//??只適用于?window平臺??
  • ????jstring?WindowsTojstring(?JNIEnv*?env,const?char*?str?)??
  • ????{???//GB2312轉換成?unicode(UTF-16)??
  • ????????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?);??
  • ????????????}??
  • ????????????//???rtn?=?(env)->NewString(??(jchar*)buffer,?length?);??
  • ????????}??
  • ????????if(?buffer?)??
  • ????????????free(?buffer?);??
  • ??????????
  • ????????return?rtn;??
  • ????}??
  • ??
  • ??
  • ????/************************************************************************/??
  • ????//java?jni中跨平臺(win32或linux,unix)??String轉為jstring?。???????????????????????????????????????????????????????????????????????
  • ????/************************************************************************/??
  • ????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));???
  • ????????//如果str為空則拋出異常給jvm??
  • ??????????
  • ????????env->SetByteArrayRegion(?bytes,?0,??strlen(str),?(jbyte*)str);???
  • ????????jstring?encoding?=?env->NewStringUTF(?"GBK");???
  • ????????jstring?strRtn?=?(jstring)env->NewObject(?strClass,?ctorID,?bytes,???
  • ????????????encoding);??
  • ????????//釋放str內存??
  • ????????//?free(str);??
  • ????????return?strRtn;??
  • ????}??
  • ??
  • ????/************************************************************************/??
  • ????//java?jni中跨平臺(win32或linux,unix)??jstring轉為String?。???????????????????????????????????????????????????????????????????????
  • ????/************************************************************************/??
  • ????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;?/*?out?of?memory?error?*/???
  • ????????}???
  • ????????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)?{???
  • ????????????????//??這個好像是要在Linux?平臺下才有的函數,??
  • ????????????????//??window?報錯:error?C2065:?'JNU_ThrowByName'?:?undeclared?identifier??
  • ????????//??????JNU_ThrowByName(env,?"java/lang/OutOfMemoryError",?0);?????
  • ????????????????env->DeleteLocalRef(bytes);???
  • ????????????????return?0;???
  • ????????????}???
  • ????????????env->GetByteArrayRegion(bytes,?0,?len,?(jbyte?*)result);???
  • ????????????result[len]?=?0;?/*?NULL-terminate?*/???
  • ????????}?else?{???
  • ????????????env->DeleteLocalRef(exc);???
  • ????????}???
  • ????????env->DeleteLocalRef(bytes);???
  • ????????return?(char*)result;??
  • ????}??
  • ??
  • }??
  • ??
  • ???
  • #endif??
  • // /* ASCII 英文一個字節 gb2312,gbk 中文兩個字節,英文一個字節 在中文系統中ansi一般指gb2312或gbk GB2312、GBK都屬于雙字節字符集 (DBCS) Utf-8 中文三個字節,英文一個字節 Unicode 中文兩個字節,英文兩個字 */ // // /* GB2312 中國國家 標準碼ANSI碼(American National Standards Institute)美國國家 標準學會 的 標準碼ASCII碼(America Standard Code for Information Interchange)美國信息交換標準碼你可以認為是不同的東西!ANSI碼僅在前126個與ASCII碼相同 */ //// /* /* * 二進制、八進制、十進制、十六進制轉換 16進制和字符相互轉換 */ //// /*java內部是使用16bit的unicode編碼(UTF-16)來表示字符串的,無論中文英文都是2字節;jni內部是使用UTF-8編碼來表示字符串的,UTF-8是變長編碼的unicode,一般ascii字符是1字節,中文是3字節;c/c++使用的是原始數據,ascii就是一個字節了,中文一般是GB2312編碼,用兩個字節來表示一個漢字。 */ //// // CodeConver.h //#ifndef CodeConver_H #define CodeConver_Hnamespace CodeConvert {#include <stdio.h>#include <string.h>#include <windows.h>#include <assert.h>#include <jni.h>#include <jni_md.h>// ANSI To Unicode// 測試通過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; }/************************************************************************/// Unicode To ANSI(美國國家標準碼)// 測試通過// [2/8/2012 liu]/************************************************************************/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;}/************************************************************************/// UTF8 To Unicode// [2/8/2012 liu]// 測試通過/************************************************************************/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;}/************************************************************************/// UnicodeToUTF8// ????????// [2/8/2012 liu]/************************************************************************/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;}/************************************************************************/// 寬字符 轉換為 多字符 Unicode - ANSI// ????????// [2/8/2012 liu]/************************************************************************/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;}/************************************************************************/// 多字符 轉換為 寬字符 ANSI - Unicode// // [2/8/2012 liu]/************************************************************************/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;}/************************************************************************/// ANSI To UTF8// ??????// [2/8/2012 liu]/************************************************************************/char* ANSIToUTF8(const char* str){return UnicodeToUTF8(ANSIToUnicode(str));}/************************************************************************/// UTF8 To ANSI// 測試通過// [2/8/2012 liu]/************************************************************************/char* UTF8ToANSI(const char* str){wchar_t * temp;if (str == NULL){return NULL;}UTF8ToUnicode(temp,str);return UnicodeToANSI(temp);}// 測試OK// UTF8 convert to GB2312void 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;// delete [] pszGB2312;// delete [] pwszGB2312;}// GB2312 convert to UTF8// 測試通過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;// delete [] pszUTF8;// delete [] pwszUTF8;}// Gb2312 To Unicode// 測試通過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;}/////*************************************************************************** * 函數名: HexToString * * 描 述: 16進制 轉為 字符串 * * 參 數: IN:const char *pHex,IN:unsigned long hexLen,* OUT:char *pByteString * * 返 回: 成功:0 , 失敗:1、2 * * 作 者: liuguanglin ***************************************************************************/DWORD HexToString(/*IN*/ const char * pHex,/*IN*/ DWORD hexLen,/*OUT*/ 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; // 小寫:0x37 改為 0x57 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; // 小寫:0x37 改為 0x57 }return 0;}/*************************************************************************** * 函數名: StringToHex * * 描 述: 字符串 轉為 16進制 * * 參 數: IN:const char *pHexBYTE,IN:DWORD dwStrLen,OUT:BYTE *pbHex * * 返 回: 成功:0 , 失敗:1、2 * * 作 者: liuguanglin***************************************************************************/DWORD StringToHex(/*IN*/ const BYTE * pByteString,/*IN */ DWORD dwStrLen,/*OUT*/ 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;elsereturn 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;elsereturn 4;}} }return 0;}/*****************************************HEX 到 ASCII 的轉換函數入口參數: data: 轉換數據的入口指針buffer: 轉換后數據入口指針len : 需要轉換的長度返回參數:轉換后數據長度*******************************************/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;}/*****************************************ASCII 到 HEX 的轉換函數入口參數: O_data: 轉換數據的入口指針,N_data: 轉換后新數據的入口指針len : 需要轉換的長度返回參數:-1: 轉換失敗其它:轉換后數據長度注意:O_data[]數組中的數據在轉換過程中會被修改。****************************************/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')) //A....F{tmpData = O_data[i] - 0x37;}else if((O_data[i] >= 'a') && (O_data[i] <= 'f')) //a....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;}/*************************************************************************** * 函數名: Soft_swap * * 描 述: RSA密鑰 倒序 * * 參 數: IN: void *dst, IN: const void *src, IN: size_t count * * 返 回: 無 * * 作 者: liuguanglin**************************************************************************/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);}}// 編碼轉換 UTF8 convert to GB2312VOID UTF82GB2312( /*IN*/ const char * utf8, /* OUT */ 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;}GB2312 convert to UTF8VOID GB23122UTF8( /*IN*/ const char * gb2312, /* OUT */ 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;} //----------------------------------------------- JNI 部分 --------------------------------------- /*java內部是使用16bit的unicode編碼(UTF-16)來表示字符串的,無論中文英文都是2字節;jni內部是使用UTF-8編碼來表示字符串的,UTF-8是變長編碼的unicode,一般ascii字符是1字節,中文是3字節;c/c++使用的是原始數據,ascii就是一個字節了,中文一般是GB2312編碼,用兩個字節來表示一個漢字。 */// 測試Ok// 只適用于 window平臺char* jstringToWindows( JNIEnv *env, jstring jstr ){ // unicode(UTF-16) 轉換成 GB2312int 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;}// 測試Ok// 只適用于 window平臺jstring WindowsTojstring( JNIEnv* env,const char* str ){ //GB2312轉換成 unicode(UTF-16)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 );}// rtn = (env)->NewString( (jchar*)buffer, length );}if( buffer )free( buffer );return rtn;}/************************************************************************///java jni中跨平臺(win32或linux,unix) String轉為jstring 。 /************************************************************************/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)); //如果str為空則拋出異常給jvmenv->SetByteArrayRegion( bytes, 0, strlen(str), (jbyte*)str); jstring encoding = env->NewStringUTF( "GBK"); jstring strRtn = (jstring)env->NewObject( strClass, ctorID, bytes, encoding);//釋放str內存// free(str);return strRtn;}/************************************************************************///java jni中跨平臺(win32或linux,unix) jstring轉為String 。 /************************************************************************/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; /* out of memory error */ } 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) { // 這個好像是要在Linux 平臺下才有的函數,// window 報錯:error C2065: 'JNU_ThrowByName' : undeclared identifier// JNU_ThrowByName(env, "java/lang/OutOfMemoryError", 0); env->DeleteLocalRef(bytes); return 0; } env->GetByteArrayRegion(bytes, 0, len, (jbyte *)result); result[len] = 0; /* NULL-terminate */ } else { env->DeleteLocalRef(exc); } env->DeleteLocalRef(bytes); return (char*)result;}}#endif


    總結

    以上是生活随笔為你收集整理的字符编码转换_进制转换(GB2312,GBK,JNI,HexTOStr)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。