程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C# C++ Java接口類型轉換,

C# C++ Java接口類型轉換,

編輯:C++入門知識

C# C++ Java接口類型轉換,


  最近這幾天做了一個兼職 ,主要是把C語言以及C#語言封裝的dll,經過C++中轉為Java語言支持的,主要其中的問題就是類型轉換,在此列出常用類型的轉換過程。

 1 #include "stdafx.h"
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <iostream>
 5 #include "www_cnblogs_com\cyjwdm0503.h"
 6 #include <malloc.h>
 7 #include <Windows.h>
 8 
 9 using namespace std;
10 using namespace  System::Runtime::InteropServices;
11 using namespace  System;
12 
13 //char* ->c#
14 System::String^ charptrtoCstring( const char* str )
15 {
16     System::String^ str1  =  Marshal::PtrToStringAnsi((IntPtr)(char*)str);
17     return str1;
18 }
19 
20 
21 //C#->char*
22 char* Cstringtocharptr( System::String^ s )
23 {
24     char* buffer = (char*)(void*)Marshal::StringToHGlobalAnsi(s);
25     return buffer;
26 }
27 
28 
29 //jstring ->char*
30 char* jstringTocharptr( JNIEnv *env, jstring jstr )
31 { //UTF8/16轉換成gb2312
32     int length = (env)->GetStringLength(jstr );
33     const jchar* jcstr = (env)->GetStringChars(jstr, 0 );
34     char* rtn = (char*)malloc( length*2+1 );
35     int size = 0;
36     size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL );
37     if( size <= 0 )
38         return NULL;
39     (env)->ReleaseStringChars(jstr, jcstr );
40     rtn[size] = 0;
41     return rtn;
42 }
43 
44 //char* ->jstring
45 jstring charptrTojstring( JNIEnv* env, const char* str )
46 {//gb2312轉換成utf8/16
47     jstring rtn = 0;
48     int slen = strlen(str);
49     unsigned short * buffer = 0;
50     if( slen == 0 )
51         rtn = (env)->NewStringUTF(str );
52     else
53     {
54         int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );
55         buffer = (unsigned short *)malloc( length*2 + 1 );
56         if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length ) >0 )
57             rtn = (env)->NewString(  (jchar*)buffer, length );
58     }
59     if( buffer )
60         free( buffer );
61     return rtn;
62 }
63 
64 //jchar ->char
65 char jchartochar( jchar c )
66 {
67     char ch = (char)(c & 0x00ff);
68     return ch;
69 }
70 
71 //char* ->jchar
72 jchar charTojchar( char c )
73 {
74     jchar ch = (jchar)(c | 0xff00);
75     return ch;
76 }
77 
78 //char ->C#
79 System::Char chartoChar( char c )
80 {
81     System::Char C = (System::Char)((int)(c));
82     return C;
83 }
84 
85 //C#->char
86 char Chartochar( System::Char c )
87 {
88     char C = (char)((int)(c));
89     return C;
90 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved